#include <stdio.h>
#include <string.h>
void main(){
char sss[0]; //array with 0 elements
sss[0]= 'h'; sss[1]= 'o'; sss[2]= 'w'; //how does this line compile wihtout error?
printf("sss after 3 chars added: %s\n", sss);
strcpy(sss, "n");
printf("sss after strcpy: %s\n", sss);
strcat(sss, " stuff");
printf("sss after strcat: %s\n", sss);
}
Here, I declared a character array 'sss' with a size of 0. Thus, it wouldn't be able to assign any char to any elements. However, the array behaves like a dynamically allocated one, allowing assignment of any number of chars. The code above produces the following output.
sss after 3 chars added: how
sss after strcpy: n
sss after strcat: n stuff
I thought C was strict with array allocations and expected it to throw "array size out of range" error. Why/how is this happening?