1

I'm new to c programming and wanted to write a helper function for myself that should do the following

  • Receiving a char** as first input parameter
  • Receiving const char* as second input parameter (path to file)
  • Reading in the file and populating char** with the read in lines
  • Returning the number of lines read in so that the calling function "knows" the size of the passed in array

The idea is to have a helper function that will do the job of reading in a file without needing to rewrite the logic all the time again.

I continuously received segmentation faults and therefore reduced this for debugging to the following test case (still not working):

Test function:

int test_read_in_file() {
    char **arrayLinesOfFile;
    int numberOfRowsReadIn = read_in_file("test_file.txt",     
    arrayLinesOfFile);
    printf("%s\n", arrayLinesOfFile[0]); // gives segmentation fault
}

The function (reduced to a simple test for now):

int read_in_file(const char* pathToFile, char** linesOfFile) {
    int numberOfRows = 5;
    linesOfFile = (char**) malloc(numberOfRows * sizeof(char*));
    linesOfFile[0] = malloc(10 * sizeof(char)); // just for testing
    strcpy(linesOfFile[0], "Andreas"); // just for testing
    printf("%s\n",linesOfFile[0]); // this works fine, output "Andreas"
    return numberOfRows;
}

When running this test case, the printf statement in the function works fine, the printf in the test runner gives a segmentation fault.

I don't understand why this is happening. In my understanding linesOfFile would be passed by references, then I allocate memory for the first element and then sufficient elements to hold "Andreas" in the first element.

Any help would be highly appreciated.

Thanks Andreas

1 Answers1

2

C uses pass-by-value. You need to pass the address of arrayLinesOfFile so that allocated memory is reflected in the calling function.

In other words, when you pass arrayLinesOfFile to read_in_file, a local pointer to a pointer to a char is made named linesOfFile in read_in_file and it points to the location where arrayLinesOfFile points to. When you use

linesOfFile = (char**) malloc(numberOfRows * sizeof(char*));

linesOfFile is made to point to the start of the allocated memory segment. This doesn't have any effect on arrayLinesOfFile in the other function. So, arrayLinesOfFile remains uninitialized. This explains the seg-faults.


So change

int numberOfRowsReadIn = read_in_file("test_file.txt",     
arrayLinesOfFile);

to

int numberOfRowsReadIn = read_in_file("test_file.txt",     
&arrayLinesOfFile);

and the read_in_file function to:

int read_in_file(const char* pathToFile, char*** linesOfFile) {
    int numberOfRows = 5;
    *linesOfFile = malloc(numberOfRows * sizeof(char*));
    (*linesOfFile)[0] = malloc(10 * sizeof(char)); // just for testing
    strcpy((*linesOfFile)[0], "Andreas"); // just for testing
    printf("%s\n",(*linesOfFile)[0]); // this works fine, output "Andreas"
    return numberOfRows;
}

Other changes I made in the code are

Community
  • 1
  • 1
Spikatrix
  • 20,225
  • 7
  • 37
  • 83