Bear with me as I am adding some minor, secondary questions too instead of posting those separately
In a declaration char name[]="Germ";
the identifier Germ is of type char[5], right? But in an assignment like ptr="Germ"
, where ptr had been declared as a character pointer, "Germ" acts as a character pointer, right?
So here are my confusions which I request you to clear. Even one liners would be helpful:
1) Why is "Germ" an array object in declaration but a pointer in second? Should I conclude that a particular syntax has different meaning during declaration than in other statements? For example "{'a','b','c'}" is an initializer for an array and not a compound statement even though enclosed in curly brackets...
2) In the first declaration of this question, are "&name" and "name" of different types in that "&name" is the address of an array object of size 5 while "name" is the address of a character variable, i.e, the address of the first element of the array called name? I feel it is so, but want your confirmation.
3) And finally, if modifying strings is UB in C (I read in a good book), how come the following code doesn't show even a warning and prints "ariund"?
#include<stdio.h>
int main()
{
char str[]="around";
str[2]='i';
printf("%s",str);
return 0;
}
You answers will be very much appreciated.