0

I'm working on some code to read a file that contains a list of users, their information, and put the information of all the users into a linked list. Two parameters of the user struct is a pointer to a linked list of locals, and a pointer to a linked list of points of interest.

Each line of the file is a different user.

The program is fully working for everything - except for the linked list of locals. My process is to create a linked list of locals for each line of the file, and then pass the pointer to the first node of that list (together of the rest of the information from the user of that line) to the function insertpersonlist to add that user and it's information to the linked list.

Each linked list of locals is working fine BEFORE I save it together with the rest of the user information into the linked list of users. After passing it to the function insertpersonlist, I get segmentation fault when trying to access the list of locals.

I know the code is a bit big, but any help is really appreciated . Thanks in advance.

typedef struct pdi{
    char *name;
    char *descript;
    char *schedule;
    int id;
    int popp;
    int hotpoints;
    struct pdi *prox;
}t_pdi;

typedef struct local{
    int id;
    char *name;
    int popl;
    struct local *prox;

}t_local;

typedef struct pessoa{
    char *name;
    char *address;
    char *dob;
    int phonenumber;
    t_pdi *pdis;
    t_local *local;
    struct pessoa *prox;
}t_person;




void crialistapessoas(t_person *header_p, t_pdi *header_pdi, t_local *header_l){
    FILE *fp;
    t_person *node = header_p;
    t_pdi *new1listpdi = create_headerPDI();
    t_pdi *nodepdi = new1listpdi;
    t_local *new1L;
    t_pdi *new1PDI;
    t_local *new1listlocals = create_headerL();
    t_local *nodelocal = new1listlocals;
    t_local *tempnodel;
    t_pdi *tempnodepdi;


    char *name = malloc(200*sizeof(char));
    char *line = malloc(200*sizeof(char));
    char *address = malloc(200*sizeof(char));
    char *dob = malloc(200*sizeof(char));

    int phonenumber;
    int tempid,i, n;
    fp = fopen("names.txt", "r");
    fgets(line, 150, fp);
    while(strcmp(line, "end")!=0){
        name = strtok(line, "/");
        address = strtok(NULL, "/");
        dob = strtok(NULL, "/");
        phonenumber = atoi(strtok(NULL, "/"));
        new1listpdi = create_headerPDI();
        new1listlocals = create_headerL();
        nodepdi = new1listpdi;
        nodelocal = new1listlocals;
        n = atoi(strtok(NULL,"/"));


        for(i=0;i<n;i++){
            tempid = atoi(strtok(NULL,"/"));
            printf("%d\n", tempid);

            if(tempid>1000){
                tempnodel = header_l;
                while(tempnodel!=NULL && tempnodel->id != tempid){
                    tempnodel = tempnodel->next;
                }
                new1L = create_headerL();
                new1L->id=tempid;
                printf("%d\n", tempnodel->id);
                new1L->name = strdup(tempnodel->name);

                new1L->popl = tempnodel->popl;
                new1L->next = nodelocal->next;
                nodelocal->next = new1L;
            }
            else{
                tempnodepdi = header_pdi;

                while(tempnodepdi!=NULL && tempnodepdi->id != tempid){
                    tempnodepdi = tempnodepdi->next;
                }
                new1PDI = create_headerPDI();
                new1PDI->id = tempid;
                new1PDI->name = strdup(tempnodepdi->name);
                new1PDI->descript = strdup(tempnodepdi->descript);
                new1PDI->schedule = strdup(tempnodepdi->schedule);
                new1PDI->hotpoints = tempnodepdi->hotpoints;
                new1PDI->popp = tempnodepdi->popp;
                new1PDI->next = nodepdi->next;
                nodepdi->next = new1PDI;
            }
        }
        insertpersonlist(header_p, name, address, dob, phonenumber, new1listlocals, new1listpdi);
        fgets(line, 150, fp);
    }
}



t_person *insertpersonlist(t_person *header_p, char *name, char *address, char *dob, int phonenumber, t_local *listlocals, t_pdi *listpdis){
    t_person *new1;
    t_person *node = header_p;
    new1 = create_headerPerson();
    if (new1== NULL)
        return NULL;
    while((node->next!=NULL)&&(strcmp(node->next->name, name)<0)){
        node = node->next;
    }
    new1->name = strdup(name);
    new1->address = strdup(address);
    new1->dob = strdup(dob);
    new1->phonenumber = phonenumber;
    new1->pdis = listpdis;
    new1->local = listlocals;
    new1->next = node->next;
    node->next = new1;
}

  • There isn't enough information to debug your problem, it could be any number of things. [You can find the line which is tripping the segfault](https://stackoverflow.com/questions/2876357/determine-the-line-of-code-that-causes-a-segmentation-fault#2876374). Though it will likely not be what is *causing* the segfault, it will help you debug or add more information to the question. – Schwern May 19 '19 at 17:25
  • OT: regarding this kind of statement: `char *name = malloc(200*sizeof(char));` The expression: `sizeof(char)` is defined in the C standard as 1. Multiplying anything by 1 has no effect and just clutters the code. Suggest removing that expression – user3629249 May 19 '19 at 20:49
  • The posted code does not compile! it is missing the needed `#include` statements for the needed header files. Are you expecting us to guess as to which header files you actual code is using? – user3629249 May 19 '19 at 20:51
  • 1
    after inserting: `#include ` and `#include ` the code still does not compile. One of the error messages from the compiler: "untitled.c:127:9: error: ‘t_person {aka struct pessoa}’ has no member named ‘next’" – user3629249 May 19 '19 at 20:58

0 Answers0