I keep passing in and returning the dirs_later_array. When I get to \"new_size=...\" in the else block, I end up with new_size of 2 the second time around. So far so good.
Your sizeof(struct dirs_later*)
should be changed to sizeof(struct dirs_later)
- as before!
Also the sizeof
is a compile time feature. You need a structure like this to hold the size
struct my_dirs
struct dirs_later *dirs;
int size;
};
Initialise it like this
struct my_dirs directories;
directories.size = 0;
directories.dirs = NULL;
Then to add (note realloc
can take NULL as a parameter
directories.dirs = realloc(directories.dirs,
(++directories.size) * sizeof(struct dirs_later));
This would also simplify your code.
Operator sizeof
is a compile time feature and it only checks the static size of an expression. So for pointer it only returns the size of that pointer which is 4 on your platform. sizeof
does not measure the size of a dynamically allocated data. There is no standard feature in C to get the size of dynamically allocated data.