Hi there,
I am reading a file that contains both numbers and strings; by reading it sequentially, everything goes well when reading floats and assigning their values to an array previously declared, BUT, when I read strings and try to assign them to a pointer defined as
and allocated asCode:const char *problem[2];
I obtain a segnemtation fault at the moment of reading the values.Code:for(i=0; i<2; ++i ) problem[i] = (char*) malloc(12 * sizeof(char));
The error is avoided ONLY if I assing the string to simple strings instead of the elements of a array of strings.
Could anyone help?
I get a segmentation fault when printing it to file (if I comment print I dont)Code://Declare: char header[12]; const char *problem[2]; //Allocate: for(i=0; i<2; ++i ) problem[i] = (char*) malloc(12 * sizeof(char)); //Read file: file_ID = fopen(input_file, "r"); fscanf(file_ID, "%s %s %s\n", header, &problem[0], &problem[1]); printf(" Verify problem 0: %s\n", problem[0]); printf(" Verify problem 1: %s\n", problem[1]);
the file to be read is the following:
Thank you very much in advanceCode:problem: problem_name problem_type
All the best
S.M.



LinkBack URL
About LinkBacks



