I have the following struct in C.
struct a {
long state;
long uid;
long w, x, y, z, xx, yy, zz, xxx, yyy, zzz;
char comm[64];
};
Then I do a malloc as follows.
buf = malloc (100 * sizeof(struct a));
But when I try to access the individual structs as follows, I get a seg fault.
for (i = 0; i < 100; ++i) {
tmp = buf + (i * sizeof(struct a));
printf ("\t>%d>%ld,%ld\n", i, tmp->state, tmp->uid);
}
I am getting a seg fault after the first 10 entries. I have no idea why this happens. Please help.
if buf is a pointer to a struct a, the pointer math should be: