When I want to store dynamic array of data in a C structure, there are two ways to write it:
typedef struct {
int row;
int col;
char* data;
} item1;
Or
typedef struct {
int row;
int col;
char data[];
} item2;
Both of them will work. But they got some differences on my 64bit Mac OSX, with gcc Apple LLVM version 5.1 (clang-503.0.38):
sizeof(item1) is 16
sizeof(item2) is 8
Why is the difference? And, what else are different for the two implementations?
The full testing code is:
#include <stdio.h>
typedef struct {
int row;
int col;
char* data;
} item1;
typedef struct {
int row;
int col;
char data[];
} item2;
int main() {
printf("%d %d\n", sizeof(item1), sizeof(item2));
return 0;
}
The output is:
16 8