+ 1
Why '&' is not used in scanf if want to take string as input from the user?
char str[]; scanf("%s",str)
5 Answers
+ 6
str is already a pointer to first element in char array . So when you pass it to scanf you are already passing the address of the first element in array to scanf and thus no need of & to get the address.
+ 3
Hi, please link your complete code and tag the relevant programming language.
+ 3
First of all, you cannot write char str[]; without specifying the size
Second, it's a terrible idea to scanf a string, use fgets
Now let's see the meaning of & and why "strings" don't need it
(this is not an easy subject, you should study it very carefully)
When you write
char c = 'a';
You have a byte in memory that holds the value 'a'
&c tells you where c is in memory
When you write
char str[] = "Hello"
(which is syntactic sugar for
char str[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
)
You are allocating 6 bytes in memory, and their values are:
'H' 'e' 'l' 'l' 'o' '\0'
You now have to think of them as if you declared 6 different unnamed char variables
str is basically a pointer (variable that holds memory addresses) and its value is the position of where the string begins