This is C++ code which shows a compiler warning but runs fine. The expected behavior was as below, but:
char sz[] = "Hello World";
char *p;
snprintf(p, 12, sz);
printf("%s", p);
The above code when ran, it crashes as the *p is not allocated. Good.
char sz[] = "Hello World";
char p[0];
snprintf(p, 12, sz);
printf("%s", p);
The above code works fine and will display "Hello World" without any errors. While compiling the above show a compiler warning it is illegal to use [0], but runs fine.
Why it is so?