I’m pretty new in C and having some problems with all the pointer stuff.
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 an error this way and just cant understand why.
I know that edgeitem and edge are tags and I can use struct edge *next but I declared the pointers so how come 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 cant understand, it’s a pointer so why do I add the *?
And the last question is:
If I’m using the above, what’s the difference between:
*EDGE next
EDGE *next
and last one :
if I’m adding:
typedef struct edge_list
{
EDGE *head;
}EDGE_LIST;
is it the same as :
pEDGE head;
You cannot use pEDGE within the definition of the struct. You shoud do something like:
You must also note that
edge_itemis a double pointer. You also mention that in your question. So if you usepEDGE_ITEMand you just want to have a normal pointer you should not writepEDGE_ITEM *edge_itembut justpEDGE_ITEM edge_item.For clarifying, all the following declarations are equivalent:
But
is equivalent to
About
*EDGE nextthat is wrong syntax. The correct syntax would beEDGE* nextorpEDGE next. So oncestruct edgeis defined you can just use any of these two, but while defining the struct, you must do as I show at the beginning of my answer.Yes, the following two definitions are equivalent: