not a structure or union error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • burak aktas
    New Member
    • Mar 2011
    • 2

    not a structure or union error

    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. here is the code;

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include "PR-ADT.h"
    
    void add_Node(LIST** , NODE*);
    LIST* create_list(void);
    
    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;
    }
    
    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;
            }
        }
    
    }
    it gives the errors in add_Node function in all lines which include
    Code:
    *(list)
    i try to add the first node only. so if i can manage to solve this problem i can continue on my work : ).
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Check your operator precedences, -> has a higher precedence than * and is applied first but list is not a pointer to a structure so the -> operator is not valid for it.

    Using (list) does nothing and is a pointless waste of parentheses, you need to use parentheses to override the normal operator precedence by using (*list).

    Comment

    • burak aktas
      New Member
      • Mar 2011
      • 2

      #3
      mate it worked. but i have a question this is my NODE and LIST structures;

      Code:
      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;
      Code:
      LIST* list
      you say "list is not a pointer to a structure" . isn't this list includes 3 pointer to structures with names(head,tail ,current). could you explain why list is not pointer to a structure, i am confused : )_?

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        in add_node void add_Node(LIST** list, NODE* student) list is declared as LIST **. So list is not a pointer to a structure, it is a pointer to a pointer to a structure. To declare a pointer to a structure you would need LIST* list (not that that would be right for your code.

        If list is a pointer to a pointer to a structure then it needs to be dereferenced twice to get a structure. Both * and -> dereference a pointer, but * dereferences any pointer and -> dereferences a pointer to a structure so you need the * to be applied first because *list is a pointer to a structure.

        Comment

        Working...