First of all, byte arrays (char) are stored in C-lines and should be allocated. In this situation, the line used to read the file should be allocated for MAX_LENGTH + 1 (+1 for the line character, \ 0):
char* string = malloc( (MAX_LENGTH+1) * sizeof(char) );
This will allocate enough memory for the maximum length string: MAX_LENGTH.
Another problem is that the array of char **picks pointers is not allocated to store 18 lines that you expect to read:
It should be allocated for 15 char pointers (char *), which should also be allocated in the first loop.
int main( int argc,char *argv[] ) { ... char* string = malloc( (MAX_LENGTH+1) * sizeof(char) ); char** picks = malloc(15*sizeof(char *)); FILE* pick_file = fopen( argv[l], "r" ); int num_picks; for( num_picks=0 ; fgets( string, MAX_LENGTH, pick_file ) != NULL ; num_picks++ ) { printf("pick a/an %s ", string );
You also need to check the return value of malloc (), and you may need to check if the contents of the file are really expected and do not contain more lines or lines longer than 15 characters.
Also scanf () reads standard input, I replaced it with sscanf () and added the missing "%" sign in the second printf ().
source share