readyyy
asked on
Beginner problem with scanf
Hello
I am a beginner in c coding and have a problem with compiling a sourcecode:
#include <stdio.h>
int main()
{
char me[20];
printf("What is your name?");
scanf("%s",&me);
printf("Darn glad to meet you, %s!\n",me);
return(0);
}
So when I try to compile this with the miracle c ver. 3.2 software, there is an error message:
e:\programme\miracle c\exercises\2\whoru.c: line8: & non lvalue
'scanf("%s",&me)'
aborting compile
So what's going on here? Thanks for any help
I am a beginner in c coding and have a problem with compiling a sourcecode:
#include <stdio.h>
int main()
{
char me[20];
printf("What is your name?");
scanf("%s",&me);
printf("Darn glad to meet you, %s!\n",me);
return(0);
}
So when I try to compile this with the miracle c ver. 3.2 software, there is an error message:
e:\programme\miracle c\exercises\2\whoru.c: line8: & non lvalue
'scanf("%s",&me)'
aborting compile
So what's going on here? Thanks for any help
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
readyyy ,
> scanf("%s",&me);
You don't need an ampersand before "me", since by doing
>char me[20]; ,"me" itself is an address.
"&" operator in C is for getting the address for a particular variable.
But in case of string (arrays) , the name of the array is the address too..
Therefore
scanf("%s",me); will work.
Thanks!
> scanf("%s",&me);
You don't need an ampersand before "me", since by doing
>char me[20]; ,"me" itself is an address.
"&" operator in C is for getting the address for a particular variable.
But in case of string (arrays) , the name of the array is the address too..
Therefore
scanf("%s",me); will work.
Thanks!
// proper code
#include <stdio.h>
int main()
{
char me[20];
printf("What is your name?");
scanf("%s",me); // & should be dropped for char []
printf("Darn glad to meet you, %s!\n",me);
return(0);
}
#include <stdio.h>
int main()
{
char me[20];
printf("What is your name?");
scanf("%s",me); // & should be dropped for char []
printf("Darn glad to meet you, %s!\n",me);
return(0);
}
>> scanf("%^\ns",me);
I think you mean
scanf("%[^\n]",me);
Paul
I think you mean
scanf("%[^\n]",me);
Paul
Thanks Paul.. you are right.. I had forgotten the exact syntax
Its the one I have to look up too often because it's obscure. I just happened to look it up a couple of days ago. Its like typedefing functions for qsort. I always have to look those up.
Paul
Paul
scanf("%s",me); is correct...
But you have still good way of doing that...
use
gets(me); instead of scanf statement.. that will read the string even if that contains spaces also...
Also, you can use
scanf("%^\ns",me);
to get the same effect... C is like SEA :)
Bye
---
Harish