I am trying to write binary data to a file. My program below:
#include <stdlib.h>
#include <stdio.h>
struct tinner {
int j;
int k[3];
};
struct touter {
int i;
struct tinner *inner;
};
int main() {
struct touter data;
data.i = 10;
struct tinner in;
in.j = 5;
in.k[0] = 1, in.k[1] = 2, in.k[2] = 3;
data.inner = ∈
printf("sizeof(data)=%d, sizeof(in)=%d\n", sizeof(data), sizeof(in));
FILE *fp = fopen("test.dat", "w+b");
fwrite((const void *) &data, sizeof(data), 1, fp);
fflush(fp);
fclose(fp);
}
The output that I get is:
sizeof(data)=8, sizeof(in)=16
Why is it so ? Why is the full structure that is hold by the variable 'data' not written to the file ? What can I do so that full data is written that is when I write instances of touter, touter.tinner is also written ?
-Satish