GeekInterview.com
   Home |  Tech FAQ  |   Interview Questions |  Placement Papers |  Tech Articles |  Learn |  Freelance Projects |  Online Testing |  Geeks Talk |  Job Postings |  Knowledge Base | Site Search |  Add/Ask Question

GeekInterview.com  >  Tech FAQs  >  Concepts
Go To First  |  Previous Question  |  Next Question 
 Concepts  |  Question 72 of 81    Print  
Why dont we give a & before reading a string in C language?
scanf("%s",str); why is it not scanf("%s",&str);

  
Total Answers and Comments: 3 Last Update: February 15, 2007     Asked by: cindy7alex 
  
 Sponsored Links

 
 Best Rated Answer

No best answer available. Please pick the good answer available or submit your answer.
November 22, 2006 01:55:09   #1  
Vamshi        

RE: Why dont we give a & before reading a string in C ...
Because in C string is a array of char. and when u say:char str[16]; or char* str; //remember to use malloc to allocate address here.str gives the starting address of the string and scanf requires address of the string where it can store the rxed data.
 
Is this answer useful? Yes | No
January 05, 2007 04:03:22   #2  
Tanu        

RE: Why dont we give a & before reading a string in C ...

In C a string is an array of characters. And as known the name of an array itself points to the base address of the array , so does the name of the string.Also an '&' is used only to give the address of the following variable. Hence , when the task of '&' is being accomplished by the string name only, so we do not need an '&'


 
Is this answer useful? Yes | No
February 15, 2007 07:55:38   #3  
winny gupta        

RE: Why dont we give a & before reading a string in C ...
We don't give an & before reading a string. This is so because a string is a character array. For an array, be it integer or character or any other array, declared as int a[10] or char str[20], the name 'a' or 'str' stores the base address of the array, that is the address of the memory location where the array is starting. The scanf statement requires the address of the location to store the value input by the user. Since str already stores the address of the memory location we do not need to add the & before it.
 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
Sponsored Links

 




About Us  |   Privacy Policy  |   Terms and Conditions  |   Contact  |   Site Map  |   Add Question  |   Propose Category  |   RSS Feeds  |   Articles Sitemap  |   Site Updates  |   Add Resource

Copyright © 2005 - 2008 GeekInterview.com. All Rights Reserved
Page copy protected against web site content infringement by Copyscape