473,498 Members | 1,648 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

not a structure or union error

2 New Member
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;

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "PR-ADT.h"
  5.  
  6. void add_Node(LIST** , NODE*);
  7. LIST* create_list(void);
  8.  
  9. int main( void ) {
  10.  
  11.     LIST* list;
  12.     NODE* student;
  13.  
  14.     list = create_list();
  15.  
  16.     FILE* myfile;
  17.     char file_name[50];
  18.     char sentence[200];
  19.  
  20.     printf("Enter the name of the file you want to create your list: ");
  21.     gets(file_name);
  22.     printf("\n");
  23.  
  24.     myfile = fopen(file_name,"r");
  25.  
  26.     while ( myfile == NULL ) {
  27.  
  28.         printf("File cannot be opened!!! Enter a new filename: ");
  29.         gets(file_name);
  30.         printf("\n");
  31.  
  32.         myfile = fopen(file_name,"r" );
  33.     }
  34.  
  35.     while ( !feof(myfile) ) {
  36.  
  37.         fgets(sentence,200,myfile );
  38.         puts(sentence);
  39.     }
  40.  
  41.     rewind(myfile);
  42.  
  43.     student = (NODE*) malloc(sizeof(NODE));
  44.  
  45.     while ( !feof(myfile) ) {
  46.  
  47.         fscanf(myfile,"%s", &(student->first_name) );
  48.         fscanf(myfile,"%s", &(student->last_name) );
  49.         fscanf(myfile,"%s", &(student->email_addr) );
  50.         fscanf(myfile,"%d", &(student->phone_num) );
  51.         fscanf(myfile,"%s", &(student->address) );
  52.         fscanf(myfile,"%s", &(student->city) );
  53.         fscanf(myfile,"%d", &(student->zipcode) );
  54.  
  55.     }
  56.  
  57.     add_Node(&list,student);
  58.  
  59.     printf("%s", list->head->first_name );
  60.  
  61.     return 0;
  62. }
  63.  
  64. void add_Node(LIST** list, NODE* student) {
  65.  
  66.     NODE* new_stu = student;
  67.  
  68.     new_stu = (NODE*) malloc(sizeof(NODE));
  69.  
  70.     if( !new_stu ) {
  71.         printf("ERROR NOT ENOUGH MEMORY!!!\n");
  72.         return;
  73.     }
  74.  
  75.     else {
  76.  
  77.         new_stu->fn_next = NULL;
  78.  
  79.         if( *(list)->head == NULL ) {
  80.  
  81.             new_stu->fn_next = *(list)->head;
  82.             *(list)->head = new_stu;
  83.  
  84.             printf("Adding operation is succesfull\n");
  85.  
  86.             *(list)->count++;
  87.  
  88.             return;
  89.         }
  90.     }
  91.  
  92. }
  93.  
it gives the errors in add_Node function in all lines which include
Expand|Select|Wrap|Line Numbers
  1. *(list)
i try to add the first node only. so if i can manage to solve this problem i can continue on my work : ).
Apr 30 '11 #1
3 11263
Banfa
9,065 Recognized Expert Moderator Expert
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).
Apr 30 '11 #2
burak aktas
2 New Member
mate it worked. but i have a question this is my NODE and LIST structures;

Expand|Select|Wrap|Line Numbers
  1. typedef struct node {
  2.  
  3.     int birth_date;
  4.     int zipcode;
  5.     int phone_num;
  6.     char first_name[50];
  7.     char last_name[50];
  8.     char city[50];
  9.     char address[50];
  10.     char email_addr[50];
  11.  
  12.     struct node* fn_next;
  13.     struct node* ln_next;
  14.     struct node* birdat_next;
  15.  
  16. } NODE;
  17.  
  18. typedef struct {
  19.  
  20.     int count;
  21.     NODE* head;
  22.     NODE* tail;
  23.     NODE* current;
  24.  
  25. }LIST;
Expand|Select|Wrap|Line Numbers
  1. 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 : )_?
Apr 30 '11 #3
Banfa
9,065 Recognized Expert Moderator Expert
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.
May 1 '11 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

15
2739
by: damian birchler | last post by:
Hi I'm wondering of what type a structure is. Of course, it is a _structure_, but an array isn't an _array_ either. So of what type is a structure? I'd say a pointer, am I right?
10
11361
by: tapeesh | last post by:
I created a C file say struct.c with the following structure declarations in the same file struct A { union key { int i; float f; }k1;
2
4012
by: SSM | last post by:
Hi, Does C standard comment about "Endianness" to be used to store a structure/union variables? Thanks & Regards, Mehta
8
3308
by: Charles Law | last post by:
Can anyone suggest how I would marshal a variable length structure back from an API call. Specifically, I am looking at the WaitForDebugEvent function, which returns a DEBUG_EVENT structure. ...
3
2793
by: WaterWalk | last post by:
I read from c99 std TC2 community draft, and found the following statement in 6.7.2.3: "Tw o declarations of structure, union, or enumerated types which are in different scopes or use different...
84
15769
by: Peter Olcott | last post by:
Is there anyway of doing this besides making my own string from scratch? union AnyType { std::string String; double Number; };
1
1989
by: eshuv | last post by:
Hello.. I have a problem with the code (I have a .cpp and a header file in.cpp file Idefine the fuctions and in .h file the structure ,union and enumeration , ) when I compile the code it...
6
4090
by: npd1 | last post by:
what is the difference between structure & union
3
7698
by: SRK | last post by:
Hi, I wanted to use an anonymous union within an structure something like below - struct Test { union { std::string user; //char user; std::string role; //char role;
5
2943
by: msl24 | last post by:
Thanks in advance to anyone/everyone who can help. I've written a code (see below) that works for small test values of N and M, say N = 50 and M = 30. However, I run into problems when I try to use...
0
7126
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7168
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6891
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7381
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4916
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3096
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3087
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
659
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
293
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.