Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C structure that ends with an asterisk

Tags:

c

pointers

struct

I was looking at this example and I found out that there is the declaration

struct edge
{
      int x;
      int y;
      int weight;
      struct edge *link;
}*front = NULL;

What does this actually mean? Is it possible to create a structure which is also a pointer with the name front and it NULL...?

like image 688
Vassilis De Avatar asked Dec 23 '22 06:12

Vassilis De


1 Answers

A struct is just another C type, as such, the variables it is used to define, may be created as normal instances, or pointers:

int a, *pA=NULL; //normal instance, pointer instance

struct edge
{
      int x;
      int y;
      int weight;
      struct edge *link;
}sEdge, *front = NULL; //normal instance, pointer instance

And, as with any pointer variable, needs to be pointed to owned memory before it can be safely used: (examples)

int main(void)
{

    // both variable types are handled the same way... 

    pA = &a; //point pointer variable to normal instance of `int a`
    front = &sEdge;//point pointer `front` to instance of 'struct edge'

    //allocate memory, resulting in assigned address with associated memory.
    pA = malloc(sizeof(*pA));
    front = malloc(sizeof(*front)); 
    ...

EDIT to answer question in comments:
This small example throws no error or warning. (Edit your question above, or better yet, post another question showing details of what you are seeing.)

struct edge
{
      int x;
      int y;
      int weight;
      struct edge *link;
}*front = '\0';

int main(void)
{
    struct edge tree[10];

    return 0;
}
like image 57
ryyker Avatar answered Dec 31 '22 02:12

ryyker