0

I am trying to get this to work for my assignment but I keep getting a segmentation fault error but I can't see a problem.

struct line{
        int source;
        int dest;
        int type;
        int port;
        char data[51];
};


int main(){
    struct line *dataIn;
    dataIn = malloc(sizeof(struct line));
    int nRecords = 0;

    readFile(&nRecords, dataIn);

    int i=0;
    for(i = 0; 1 < 100; i++){
        printf("Source: %d Data: %s\n", dataIn[i].source, dataIn[i].data);
    }

    return 0;
}

void readFile(int *nRecords, struct line *dataIn){
    FILE *fileIn;
    fileIn =fopen("data.txt","r");
    if (!fileIn){
        puts("File Open Error");
        return 1;
    }
    while(fscanf(fileIn, "%d:%d:%d:%d:%[^\n]", &dataIn[*nRecords].source, &dataIn[*nRecords].dest, &dataIn[*nRecords].type, &dataIn[*nRecords].port, dataIn[*nRecords].data) == 5){
        nRecords++;
        dataIn = realloc(dataIn,(*nRecords+1)*sizeof(struct line));
    }
    fclose(fileIn);
}

Also when I add the function prototype at the top:

void readFile(int*, struct line*);

I get the error:

Conflicting Types for 'readFile'

4
  • You're updating nRecords but not using that in the for loop. Why? Commented Nov 18, 2015 at 13:30
  • Your function readFile return -1 if fileIn is null but is of type void. Also make sure that the function prototype is after the struct declaration. Commented Nov 18, 2015 at 13:31
  • Why char data[51] for data? You said you where parsing a "dynamic structure". If your input is > 51 bytes, you will scribble over the stack pointer and be unable to return from readFile(). I think you need to do two things. 1. rethink the way you manage memory as this is quite convoluted and will be hard to support. 2. Stop using any form of scanf as it's dangerous. Commented Nov 18, 2015 at 13:52
  • Don't forget to free what you malloc. Commented Nov 18, 2015 at 15:20

1 Answer 1

1

C uses pass-by-value for function parameter passing. In your code, dataIn itself is passed to readFile() using pass-by-value.

Now, dataIn being a pointer itself, you can change the content of dataIn from the function, but you cannot change dataIn itself (look at the realloc()) from the function, and expect that to get reflected back to main().

So, after returning, your dataIn has just one element, as it was malloc()ed previously. Then, the for loop obviously try to access out of bound memory, creating undefined behavior.

If you want to change dataIn from the function, you need to pass a pointer to it to the function.

That said,

  • nRecords being a pointer, nRecords++; will not update the corresponding int value.

  • The void function readFile() should not have a return statement with a value like return 1;

Sign up to request clarification or add additional context in comments.

2 Comments

Also, you update nRecords, which is a pointer. You should write *nRecords++
@PaulOgilvie Thanks, added the same. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.