c - Why is scanf of a string saved to str while for the int it is being sent to the pointer of i? -
i have code:
char str [80]; int i; printf ("enter family name: "); scanf ("%79s",str); printf ("enter age: "); scanf ("%d",&i);
why scanf()
of string
saved str
while int
being sent pointer of i
?
scanf
needs passed address of variable in order modify it.
when passing array (a char
array, in case) function, array decays pointer first element.
so passing str
%s
specifier functionally same passing in &str[0]
.
this works char *
points dynamically allocated memory:
char *str = malloc(80); scanf ("%79s",str);
Comments
Post a Comment