What is the meaning of the below code? I thought that it is compilation error. But no compilation error occurs.
int main()
{
const int a=1;
printf("%c", ++a["Gyantonic"]);
}
Output in Linux a is segmentation fault. It gives a compilation error if a[1] is given in place of ++a["Gyantonic"].
How does it work?
is equivalent to:
which is equivalent to
equivalent to
"Gyantonic"[1]yields'y'and the++increments the'y'stored in the string literal and yields the result. But"Gyantonic"is a string literal and string literals cannot be modified. This is why you get the segmentation fault.