Code :
#include <stdio.h>
typedef struct{
int i;
int tab[];
}mystr;
mystr a = {1,{0,1,2}};
int main(void){
printf("%d%d%d\n",a.i,a.tab[0],a.tab[1]);
return 0;
}
C compiling :
$ gcc main.c -o main && ./main
101
C++ compiling :
$ g++ main.c -o main && ./main
main.c:8:27: error: too many initializers for ‘int [0]’
const mystr a = {1,{0,1,2}};
^
I do understand the problem, it is that tab
have no any memory allocated. But why this is ok for C ? Why do C++ doesn't automatically allocate the memory space for the tab
?
I Know that structures are similar to objects in C++ but may be there is some subtleties that I do not know about global object instantiation ?