Why dont we give a & before reading a string in C language? scanf("%s",str); why is it not scanf("%s",&str);

Questions by cindy7alex   answers by cindy7alex

Showing Answers 1 - 3 of 3 Answers

Vamshi

  • Nov 22nd, 2006
 

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.

  Was this answer useful?  Yes

Tanu

  • Jan 5th, 2007
 

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 '&'

  Was this answer useful?  Yes

winny gupta

  • Feb 15th, 2007
 

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.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions