2015.9.10 error about struct pointer in linked list

Source: Internet
Author: User

Yesterday with the structure of the pointer to write a double-linked list of the program, the compilation environment is VC6.0, before the time to write a single-linked list is also used by this compiler, but yesterday out of a let me very confusing problem, the code is as follows:

/*********************************************************** C language Implementation doubly linked list * file name: List.c*mr wan* Write Time: 2015.9.9* function: Simple operation for a doubly linked list * version: 1.0***********************************************************/#include <stdio.h> #include <stdlib.h> #define Size sizeof (node)//amount of memory per node typedef enum{false=0,true=1}bool;//Custom BOOL type typedef struct{ int val_0;//data val_0int val_1;//data val_1}dat;//node data domain typedef struct{dat dat;//node data domain struct node* prev;//predecessor node address struct node* next;//Post-drive node address}node;//Define the node structure body static unsigned int length=0;//the length of the list, that is, the number of nodes after the 0 node is out of the node* initlist (void); BOOL AddNode (node* head,dat DAT); BOOL addnodeat (node* head,unsigned int pos,dat dat), void listtest (node* Head), void main (void) {node* list=null;dat DAT; dat.val_0=1;dat.val_1=13; List=initlist (); AddNode (List,dat); Listtest (List); AddNode (List,dat); Listtest (List); AddNode (List,dat); Listtest (List);} /*********************************************************** node* initlist (void) * Function: Initialize a linked list and return the link header pointer * input : void* output: The head pointer of the linked list ******/node* initlist (void) {node* head= (node*) malloc (SIZE); if (null==head) {printf ("Request memory failed!\n"); return NULL;} else {head->prev=null; head->next=null; length=0; printf ("Linked list initialization succeeded!\n"); return Head; }}/*********************************************************** BOOL AddNode (node* head,dat DAT) * Function: Added at the end of a known linked list One node * Input: The table header pointer of the known list, data field for the node to be added * output: true--successful/false--failure ***********************************************************/ BOOL AddNode (node* head,dat DAT) {unsigned int i=0; node* P=head; node* New=null; while (i<length) {p=p->next; i++;} new= (node*) malloc (SIZE);  if (null==new) {printf ("Request memory failed!\n"); return FALSE; } else {p->next=new; new->prev=p; new->next=null; new->dat=dat; length++; return TRUE; }}/*********************************************************** BOOL addnodeat (node* Head,unsigned int Pos,DAT DAT) * Function: Add a node before the POS position in a known linked list * Input: Table header pointer of known linked list, add location pos, data field to add node * output: true--successful/false--failure ***************/bool addnodeat (node* head,unsigned int pos,dat DAT) {unsigned int i=0; node* P=head; node* New=null; if (pos>length) {printf ("POS should not be greater than length!\n of the linked list"); return FALSE;} else {while (i<pos-1) {p=p->next; i++;} While new= (node*) malloc (SIZE);       if (null==new) {printf ("Request memory failed!\n"); return FALSE;   } else {new->next=p->next;   p->next->prev=new;   p->next=new;   new->prev=p;   new->dat=dat;   length++; return TRUE; }}//else}/*********************************************************** void Listtest (NODE* Head) * Function: List of test functions * Input: Table header pointer to test list * Output: void***********************************************************/void listtest (NODE* head) {unsigned    int i=0; node* P=head;while (null!=p->next) {p=p->next;i++;p rintf (node%d in data field val_0=%d,val_1=%d\n), I,p->dat.val_0 , P->dat.val_1);} printf ("Test over,length of List=%d.\n\n\n", Length);}

As above, the compiler error:

C:\Documents and settings\administrator\ desktop \ Doubly linked list \list.c (168): Error c2037:left of ' prev ' specifies undefined struct/union ' NODE '

Compiler hint: p->next->prev=new; error in code. According to the error message, there is an undefined node structure on the left side of the prev. This I am very puzzled, Prev left is p->next, according to Reason said P->next is also node* type, P->next->prev is node* type, and new is node* type, which means that the variable types on both sides of the equal sign are consistent , but the compiler still reported the error.

I followed the compiler's hint of the error, trying to convert p->next into a forced type, as follows:

(node*) (p->next)->prev=new; compiler or prompt error, I try to put the above code should be this: ((node*) (P->next)->prev=new; This time the compiler does not error, the operation results are normal, So I don't understand, what is the difference between the two codes above? If there is a difference, it should be the difference between the priority of the operation. Then I don't understand. What is the difference between the operation precedence of the above two code. Ask the expert to answer.

The above problem first put aside, I will copy the code to Baidu know to ask for high-finger teaching, there are netizens to answer the following: I'm afraid the problem is in the structure template definition, node has not been declared, but in the structure of the body applied. or a name for the struct ...

Give the struct a name? What do you mean? I looked at the definition of the struct in my Code as follows:

typedef struct{dat dat;//node data domain struct node* prev;//predecessor node address struct node* next;//rear drive node address}node;//define node structure body

I will

struct

{

......

}

This type becomes node type, but the struct does not give him a name,

I tried to change the code to this:

typedef struct NODE{DAT dat;//node data domain struct node* prev;//predecessor node address struct node* next;//rear drive node address}node;//define node structure body

Compile without error, the result is correct. This proves that the netizen above said is correct. According to the previous error notation, the typedef just

struct

{

......

}

This type re-defines the alias node type, but before the alias is taken, the alias object is defined first, i.e. the struct is defined:

struct

{

DAT dat;//The data field of the node

struct node* prev;//Pre-node address

struct node* next;//post-drive node address

};

As we can see from the above code, in this unnamed struct, we define a struct pointer of two struct node*,

The compiler then takes the unnamed struct to the individual named node.

That is, before the struct is aliased to NODE, in the struct we have defined two struct node* pointers, so that there is no relationship between the two node, the two node is not a node, The scope of node in the struct and the re-alias to the struct is also different. In other words, the code: P->next is the structure of the node* type, the inside of the node* and struct node is not the same, the struct node has three member variables, and the structure of the node is not a member variable, so since p-> Next->prev will go wrong, because P->next is the node* type inside the struct, so p->next is not a member variable prev.

So later we will cast the p->next into the node*, this time is the structure of the struct node* cast into the same type as the outside structure, because the scope of the outside structure type is global, so when casting, although there are two kinds of node* , but because the structure inside the node* is a local variable, so the conversion is converted to the object and the outside of the structure of the same type, and the outside of the struct type is a member variable, so since the compiler does not error.

But the right thing is not so, after discussing with the classmate, the right should be written like this:

typedef struct NODE

{

DAT dat;//The data field of the node

struct node* prev;//Pre-node address

struct node* next;//post-drive node address

}node;//Define a node structure body

This means that we first define a struct

struct NODE

{

DAT dat;//The data field of the node

struct node* prev;//Pre-node address

struct node* next;//post-drive node address

}

In this structure we define two struct node* variables, because we have already declared the node before these two variables are defined, so the node inside is the same as the outside node, and then we give the outer struct an alias node, The three node is the same node. This is the right time.




2015.9.10 error about struct pointer in linked list

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.