简体   繁体   中英

Creating a pointer directly from a typedef struct definition

I want to check what the following code means. I think I am creating a pointer to a list of pointers to adjlistnode structures but I'm not sure.

Here is the code:

typedef struct adjlistnode {int node; int cost; struct adjlistnode *next;}
    **AdjMatrix;

I'm confused as to what **AdjMatrix actually is. Like I said above, I think it is a pointer to a list of pointers to adjlistnode structures but I'm not sure. Am I right in my assumption?

I think it is a pointer to a list of pointers to adjlistnode structures

No, it isn't.

AdjMatrix becomes a type representing a pointer to pointer to struct adjlistnode

As an example it can be used like:

AdjMatrix p = NULL; // p is now a pointer to pointer to struct adjlistnode

The code seems to be for building a linked list and AdjMatrix seems to be a short hand for referring to a pointer to the head pointer. It could be used like:

void addNode(AdjMatrix pHead, int node, int cost)
{
    struct adjlistnode *tmp = malloc(sizeof *tmp);
    tmp->node = node;
    tmp->cost = cost;
    tmp->next = *pHead;
    *pHead = tmp;
}

void deleteNode(AdjMatrix pHead)
{
    if (*pHead)
    {
        struct adjlistnode *tmp = *pHead;
        *pHead = tmp->next;
        free(tmp);
    }
}

int main(void) {
    struct adjlistnode *head = NULL;

    // Add nodes
    addNode(&head, 1, 2);
    addNode(&head, 3, 4);
    addNode(&head, 5, 6);

    // ... use the list

    // Delete nodes
    while(head) deleteNode(&head);

    return 0;
}

Notice that typedef of pointers is often considered bad practice. Instead it would be better to do:

typedef struct adjlistnode {int node; int cost; struct adjlistnode *next;} AdjMatrix;

and use it like:

void addNode(AdjMatrix **pHead, int node, int cost)

to make it clear the pHead is a pointer to pointer to AdjMatrix

The rules around a typedef can be simplified to the following generalization: if you have any valid variable declaration in C (without a storage class such as extern , static or register , etc.), then strapping a typedef at the front turns the variable name into a new type name, based on the type of the variable.

So here, without the typedef :

struct adjlistnode {int node; int cost; struct adjlistnode *next;}
    **AdjMatrix;

AdjMatrix is variable of type pointer to pointer to struct adjlistnode .

But in your post, because of the typedef , AdjMatrix is a name for the type pointer to pointer to struct adjlistnode .

From typedef [emphasis added]:

typedef is a reserved keyword in the C and C++ programming languages. It is used to create an alias name for another data type . 1 As such, it is often used to simplify the syntax of declaring complex data structures consisting of struct and union types, but is just as common in providing specific descriptive type names for integer data types of varying lengths.

The AdjMatrix is an alternative name of struct adjlistnode ** type, which is a pointer to pointer to struct adjlistnode .

You can use it to declare variables like this:

AdjMatrix pp_st_adjlistnode;

which means pp_st_adjlistnode is a pointer to pointer to struct adjlistnode .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM