Why “&” is not used for strings in scanf() function?
Below is syntax of Scanf. It requires two arguments:
scanf("Format Specifier", Variable Address);
Format Specifier: Type of value to expect while input
Variable Address: &variable returns the variable's memory address.
In case of a string (character array), the variable itself points to the first element of the array in question. Thus, there is no need to use the ‘&’ operator to pass the address.
C
#include<stdio.h>
int main()
{
char name[25];
scanf ("%s", name);
printf ("(Is Base address = address of first element)? \n %d",
(name == &name[0]));
}
|
Output:
(Is Base address = address of first element)?
1
Important Points
- ‘&’ is used to get the address of the variable. C does not have a string type, String is just an array of characters and an array variable stores the address of the first index location.
- By default the variable itself points to the base address and therefore to access base address of string, there is no need of adding an extra ‘&’
Last Updated :
29 Dec, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...