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; - #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
i try to add the first node only. so if i can manage to solve this problem i can continue on my work : ).
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) .
mate it worked. but i have a question this is my NODE and LIST structures; - 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;
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 : )_?
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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?
|
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;
|
by: SSM |
last post by:
Hi,
Does C standard comment about "Endianness" to be used
to store a structure/union variables?
Thanks & Regards,
Mehta
|
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.
...
|
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...
| |
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;
};
|
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...
|
by: npd1 |
last post by:
what is the difference between structure & union
|
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;
|
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...
|
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,...
| |
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,...
|
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |