Typedef structure pointer definition

I am new to C and have some problems with all pointer materials.

I wrote this code:

typedef struct edgeitem { double weight; }EDGE_ITEM, *pEDGE_ITEM; //also declaration of a pointer /************ edge that includes a pointer to the edge item, next and prev ******/ typedef struct edge { pEDGE_ITEM *edge_item; pEDGE *next; //pointer to next edge pEDGE *prev; //pointer to prev edge }EDGE, *pEDGE; 

I get the error this way and just can't figure out why.

I know that the edgeitem and edge tags are tags, and I can use struct edge *next , but I pointed pointers the way I can't use them?

Do I need to use * if I have a pointer?

 pEDGE_ITEM *edge_item //or pEDGE_ITEM edge_item 

I can’t understand, this is a pointer, so why am I adding *?

And the last question: If I use the above, what is the difference between:

 *EDGE next EDGE *next 

and last: if I add:

 typedef struct edge_list { EDGE *head; }EDGE_LIST; 

it is the same as:

 pEDGE head; 
+6
source share
3 answers

You cannot use pEDGE in a structure definition. You are doing something like:

 typedef struct edge { pEDGE_ITEM *edge_item; struct edge *next; //pointer to next edge struct edge *prev; //pointer to prev edge } EDGE, *pEDGE; 

You should also notice that edge_item is a double pointer. You also mentioned this in your question. Therefore, if you use pEDGE_ITEM and you just want to have a regular pointer, you should not write pEDGE_ITEM *edge_item , but just pEDGE_ITEM edge_item .

For clarification, all of the following declarations are equivalent:

 struct edgeitem *edge_item; EDGE_ITEM *edge_item; pEDGE_ITEM edge_item; 

But

 pEDGE_ITEM *edge_item; 

equivalently

 struct edgeitem **edge_item; EDGE_ITEM **edge_item; 

O *EDGE next , this is the wrong syntax. The correct syntax will be EDGE* next or pEDGE next . So, once the struct edge defined, you can simply use either of these two, but when defining the structure you have to do as I will show at the beginning of my answer.

Yes, the following two definitions are equivalent:

 typedef struct edge_list { EDGE *head; } EDGE_LIST; typedef struct edge_list { pEDGE head; } EDGE_LIST; 
+10
source

You use a type alias before defining it. Using a structure tag is a workaround:

 typedef struct edge { EDGE_ITEM *edge_item; struct edge *next; //pointer to next edge struct edge *prev; //pointer to prev edge } EDGE; 

Note that you almost certainly misused pointer aliases; I avoided them.

+2
source

I think this should work fine.

  typedef struct edgeitem { double weight; }EDGE_ITEM, *pEDGE_ITEM; //also declaration of a pointer /************ edge that includes a pointer to the edge item, next and prev ******/ typedef struct edge *pEDGE; //<<------- this typedef struct edge { pEDGE_ITEM *edge_item; pEDGE *next; //pointer to next edge pEDGE *prev; //pointer to prev edge }EDGE; 
0
source

Source: https://habr.com/ru/post/916759/


All Articles