hi there i am trying to build a linked list from read a file operation. this code only adds the first node to the list. the problem is i am facing with "error: request for member 'head' in something not a structure or union" error. i call add_Node function in main like this ;

add_Node(&list, student)

and this the add_Node function.

void add_Node(LIST** list, NODE* student) {

    NODE* new_stu = student;

    new_stu = (NODE*) malloc(sizeof(NODE));

    if( !new_stu ) {
        printf("ERROR NOT ENOUGH MEMORY!!!\n");
        return;
    }

    else {

        new_stu->fn_next = NULL;

        if( *(list)->head == NULL ) {

            new_stu->fn_next = (list)->head;
            *(list)->head = new_stu;

            printf("Adding operation is succesfull\n");

            *(list)->count++;

            return;
        }
    }

}

Recommended Answers

All 5 Replies

hi there i am trying to build a linked list from read a file operation. this code only adds the first node to the list. the problem is i am facing with "error: request for member 'head' in something not a structure or union" error.

Which line is it complaining about?

I noticed this: new_stu->fn_next = (list)->head; is the only line where you don't dereference list --shouldn't it be new_stu->fn_next = *(list)->head; like all the others?

Also, your code seems to be a bit confused. If you're building a linked list with next fields in each structure, there's no need for LIST** list . All you should need is LIST* list ; the next field in each structure will point to the rest of the list.

it complains about the lines which include

*(list)

and my structures are;

typedef struct node {

    int birth_date;
    int zipcode;
    int phone_num;
    char first_name[50];
    char last_name[50];
    char city[50];
    char address[50];
    char email_addr[50];

    struct node* fn_next;
    struct node* ln_next;
    struct node* birdat_next;

} NODE;

typedef struct {

    int count;
    NODE* head;
    NODE* tail;
    NODE* current;

}LIST;

and my main function is;

int main( void ) {

    LIST* list;
    NODE* student;
    
    list = create_list();

    FILE* myfile;
    char file_name[50];
    char sentence[200];

    printf("Enter the name of the file you want to create your list: ");
    gets(file_name);
    printf("\n");

    myfile = fopen(file_name,"r");

    while ( myfile == NULL ) {

        printf("File cannot be opened!!! Enter a new filename: ");
        gets(file_name);
        printf("\n");

        myfile = fopen(file_name,"r" );
    }

    while ( !feof(myfile) ) {

        fgets(sentence,200,myfile );
        puts(sentence);
    }

    rewind(myfile);

    student = (NODE*) malloc(sizeof(NODE));

    while ( !feof(myfile) ) {

        fscanf(myfile,"%s", &(student->first_name) );
        fscanf(myfile,"%s", &(student->last_name) );
        fscanf(myfile,"%s", &(student->email_addr) );
        fscanf(myfile,"%d", &(student->phone_num) );
        fscanf(myfile,"%s", &(student->address) );
        fscanf(myfile,"%s", &(student->city) );
        fscanf(myfile,"%d", &(student->zipcode) );

    }

    add_Node(&list,student);

    printf("%s", list->head->first_name );
    
    return 0;
}

i made some operations by using singly linked list. for example; adding,deleting,searching, remove duplicates etc. with passing the address of the head node to these functions which are waiting pointer to pointer. but i am new to use a linked list with two different structure types(NODE,LIST). but it shouldn't be different from singly linked list. so this the reason why i am trying with pointer to pointer.

Which line is it complaining about?

I noticed this: new_stu->fn_next = (list)->head; is the only line where you don't dereference list --shouldn't it be new_stu->fn_next = *(list)->head; like all the others?

Also, your code seems to be a bit confused. If you're building a linked list with next fields in each structure, there's no need for LIST** list . All you should need is LIST* list ; the next field in each structure will point to the rest of the list.

it complains about the lines which include

*(list)

Change it to (*list), perhaps...

Change it to (*list), perhaps...

yes it worked : )

Check out this link. It contains the operator precedence for all the operators in C

If you look at this link carefully and then look at the sequence of operations in the statement *(list)-> head you will realize your mistake

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.