1

I'm working on a script to create a linked-list of words (locals) from a file in C. Basically, I want a linked list of the first word of each line. I'm getting the error "incompatible types when assigning to type 'struct local *' from type 't_local {aka struct local}'" and can't figure out what's happening. Help would be very appreciated as I'm struggling a bit with linked lists

typedef struct local{
    char *name;
    struct local *next;

}t_local;


void crialistalocais(t_local *header){
    FILE *fp;
    t_local *aux = header->next;
    char line[150];
    char *name1;
    fp = fopen("loclss.txt","r");

    while (!feof(fp)){
        fgets(line, 100, fp);
        namel = strtok(line, '/');
        aux->name = namel;
        aux->next = *header;
        header=aux;
    }

}

4
  • header is a struct local *, therefore *header is a struct local. Commented Apr 28, 2019 at 14:35
  • 1
    (Also, your loop should not check feof, but the return value of fgets like so: while (fgets(line, sizeof(line), fp)) ... Commented Apr 28, 2019 at 14:37
  • 3
    You are doing a lot of dangerous stuff here. Where are you allocating memory for your linked list items? You are also reusing the line buffer, pointing struct members at it without copying any of the data. Commented Apr 28, 2019 at 14:41
  • Hey @Cheatah ! Thanks for the constructive criticism! Any tips on how to fix it? It's my first time having to deal with memory allocation and this type of structures, so any help would be amazing Commented Apr 28, 2019 at 14:43

1 Answer 1

3
aux->next = *header;

You're dereferencing header and trying to assign a struct local to a struct local*.

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

Comments

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.