Home > Back-end >  How to create a pointer to array of pointers pointing to nodes in c?
How to create a pointer to array of pointers pointing to nodes in c?

Time:01-25

Pointer to array of pointers pointing to nodes

I want to create a pointer to an array of pointers, and that pointer inside the array points to nodes.

CodePudding user response:

Maybe you can try to image 2D points nodes array as 1D vector. That will help you image this question.

If you want to set many groups nodes coordinate

Group 1: node1X,node1Y,node2X,node2Y,node3X,node3Y .........

Group 2: node1X,node1Y,node2X,node2Y,node3X,node3Y .........

Group 3: node1X,node1Y,node2X,node2Y,node3X,node3Y .........

.......

void main()
{
//5 groups,each group have 4 nodes
int ** groups;
groups = new int*[5];
for (int i = 0; i < 5; i  ) groups[i] = new int[4 * 2];
//set
for (int i = 0; i < 5; i  )
{
    for (int j = 0; j < 4; j  )
    {
        groups[i][j * 2] = j;
        groups[i][j * 2   1] = j;
    }
}
//printf
for (int i = 0; i < 5; i  )
{
    printf("Grop[%d]:", i);
    for (int j = 0; j < 4; j  )
    {
        printf("node[%d]X=%d ", j, groups[i][j*2] );
        printf("node[%d]Y=%d ,", j, groups[i][j*2 1]);
    }
    printf("\n");
}
for (int i = 0; i < 5; i  ) delete groups[i];
delete groups;
system("pause");
}

Remember to delete memory!

Print result sample

CodePudding user response:

you mean data_type **identifier, you could n dimension in your array declaration.

data_type **identifier = malloc(sizeof(data_type))

then for example assigning the first element of this array could be.

identifier[0] = malloc(size_of_this_array * sizeof(data_type))

  •  Tags:  
  • Related