You can't read data into a char * because a char * is a pointer to data and unless you allocate some data to it you have no-where to store the data you read.
The reason you second example works is because data has been allocated to the pointer in the assignment statement in the DATA segment of the program.
You have you pointers and arrays confused, to start with the simple ones
int size[1];
Appears a lot. There is little point in declaring an array of size 1, you may as well declare
int size;
and use the & operator to get a pointer to it when required
int readbin(char name[], char *strings[]);
This is a slightly archaich way of declaring a function and many people today believe confusing, this declaration is directly equivilent to
int readbin(char *name, char **strings);
leading on to
-
for (i = 0; i < size[0]; i++) {
-
fread(&strings[i], sizeof(char), stringlengths[i], fp);
-
strings is of type char ** so strings[i] is of type char * and &strings[i] is of type char ** (again). What this call actually does is read the data from the file and place it in the location reserved string[i] which is supposed to be a char * and there is 4 bytes in size.
Since &string[1] = &string[0] + 4
when you read the second string you over write the last 2 bytes of string[0] (y and the zero terminator. On the final write to &string[2] you write off the end of data allocated to string which my system detected and raised an exception.
fread expects a void *, you need to pass type char * (since you are using a char array) and you need to allocate data to that array. You have passed the function an array of char * not allocated to anything, this should fix it
-
for (i = 0; i < size[0]; i++) {
-
strings[i] = malloc(stringlengths[i]);
-
fread(strings[i], sizeof(char), stringlengths[i], fp);
-
}
-
But note you will need to free the memory once you have finished with it.
You have a similar error where you call printf, you pass &readstrings[0] but readstrings is of type char * array so readstrings[0] is a char * but &readstrings[0] is a char **. You pass a char ** to printf but it is expecting a char *.