Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does this pointer to pointer in a struct mean?

Tags:

c

pointers

So I was given a struct:

struct Xxx
{
    struct Yyy{...};
    Yyy **yyys;             // matrix of yyys
};

I am confused about how pointer to pointer is related to a matrix?

And how can I initialize a new Yyy and a new Xxx?

like image 864
GzAndy Avatar asked Feb 02 '16 18:02

GzAndy


1 Answers

The first level pointer would point to an array of pointers, and each second level pointer would point to an array of Yyy.

They can be set up as follows:

struct Yyy **makeMatrix(int rows, int cols)
{
    int i;
    struct Yyy **result = malloc(rows*sizeof(struct Yyy *));
    for (i = 0; i < rows; i++) {
        result[i] = malloc(cols*sizeof(struct Yyy));
    }
    return result;
}
like image 131
dbush Avatar answered Nov 05 '22 14:11

dbush