?
Solved

Beginner problem with scanf

Posted on 2005-03-08
7
Medium Priority
?
659 Views
Last Modified: 2012-05-05
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
 


0
Comment
Question by:readyyy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 16

Accepted Solution

by:
PaulCaswell earned 200 total points
ID: 13486014
Its:

           scanf("%s",&me);

scanf takes addresses of where to put the values it scans. You have defined 'mem' as an array of 20 characters. Arrays are treated as pointers in C so '&me' is actually a pointer to a pointer to the characters.

Try using:

           scanf("%s",me);

Paul
0
 
LVL 37

Expert Comment

by:Harisha M G
ID: 13486255
Hi readyyy,
    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
0
 
LVL 3

Expert Comment

by:madhurdixit
ID: 13486265
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!
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Expert Comment

by:MOH_R
ID: 13487964
// 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);

}
0
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 13487987
>>    scanf("%^\ns",me);
I think you mean
    scanf("%[^\n]",me);

Paul
0
 
LVL 37

Expert Comment

by:Harisha M G
ID: 13488170
Thanks Paul.. you are right.. I had forgotten the exact syntax
0
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 13488208
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
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question