简体   繁体   English

指向二维动态数组指针的指针

[英]Pointer to a 2D dynamic Array Pointer

How to create a pointer to the following 2D array pointer int **arr.如何创建指向以下二维数组指针 int **arr 的指针。 I am looking for something similar to int*** arr and its allocation, deallocation, accessing and usage.我正在寻找类似于 int*** arr 的东西及其分配、解除分配、访问和使用。

int main()
{
  int** arr;

  int row = 11;
  int col = 2;
  int i,j;

  arr = (int**)malloc(sizeof(int*)*row);
  for (i=0; i<row; ++i){
    arr[i] = (int*)malloc(sizeof(int)*col);
  }

  for (i = 0; i < row; i++) {
    for (j = 0; j < col; j++) {
        arr[i][j] = i;
        printf("arr[%d][%d] : %d\n",i,j,arr[i][j]);
    }
  }

  return 0;
}

Previous references:以前的参考:

Dynamic 2D array Pointer:动态二维数组指针:

C programming initialize 2D array dynamically C编程动态初始化二维数组

Pointer to dynamic 2D array, But the explanation here is using new keyword and I want to use malloc instead:指向动态二维数组的指针,但这里的解释是使用new关键字,我想改用malloc

How to allocate a 2D array of pointers in C++ 如何在 C++ 中分配一个二维指针数组

Additional Info:附加信息:

Consider following scenario:考虑以下场景:

XYZ* arr[A][B];

"XYZ" is a custom datatype, "A" & "B" are dynamic variables at runtime “XYZ”是自定义数据类型,“A”和“B”是运行时的动态变量

In this case, how can we define A & B at runtime and access arr[A][B] element values using XYZ* type.在这种情况下,我们如何在运行时定义 A & B 并使用XYZ*类型访问arr[A][B]元素值。

Further down the answer, you find more possible solutions.在答案的下方,您会找到更多可能的解决方案。

First, I want to present a solution to you, where you manage a dynamic 2d array with a 1d pointer, in case of same-length columns.首先,我想向您展示一个解决方案,在相同长度列的情况下,您可以使用一维指针管理动态二维数组。

#include <stdlib.h>
#include <stdio.h>

struct XYZ
{
    int someValue;
};

struct XYZ* GetArrayItem(struct XYZ* itemArray, int numCols, int row, int col)
{
    return &itemArray[row * numCols + col];
}

int main()
{
    int A = 5;
    int B = 4;
    struct XYZ* arr = (struct XYZ*)calloc(A*B, sizeof(struct XYZ));

    for (int i = 0; i < A; i++)
    {
        for (int j = 0; j < B; j++)
        {
            GetArrayItem(arr, B, i, j)->someValue = 1;
        }
    }

    free(arr);

    return 0;
}

With columns of different length, a double pointer might be a viable solution.对于不同长度的列,双指针可能是一个可行的解决方案。

struct XYZ
{
    int someValue;
};

int main()
{
    int i;
    int j;
    // row count
    int A = 5;
    // column count per row
    int B[] = { 3, 4, 3, 2, 4 };
    struct XYZ** arr = (struct XYZ**)calloc(A, sizeof(struct XYZ*));
    for (i = 0; i < A; i++)
    {
        // initialize column for each row
        arr[i] = (struct XYZ*)calloc(B[i], sizeof(struct XYZ));
    }

    for (i = 0; i < A; i++)
    {
        for (j = 0; j < B[i]; j++)
        {
            // access items
            arr[i][j].someValue = 1;
        }
    }

    for (i = 0; i < A; i++)
    {
        free(arr[i]);
    }

    free(arr);

    return 0;
}

However, I would advise you to create a more explicit object structure in case where you need 2d data.但是,如果您需要二维数据,我建议您创建一个更明确的对象结构。 This makes the design more explicit and the column count per row more transparent.这使得设计更加明确,每行的列数更加透明。

struct XYZ
{
    int someValue;
};

struct MyDomainSpecificRow
{
    int numColumns;
    struct XYZ* myRowData;
};

int main()
{
    int i;
    int j;
    // row count
    int A = 5;
    // column count per row
    int B[] = { 3, 4, 3, 2, 4 };
    // 1d array of rows, each containing 1d array of cells
    struct MyDomainSpecificRow* arr = (struct MyDomainSpecificRow*)calloc(A, sizeof(struct MyDomainSpecificRow));

    for (i = 0; i < A; i++)
    {
        // initialize column for each row
        arr[i].numColumns = B[i];
        arr[i].myRowData = (struct XYZ*)calloc(B[i], sizeof(struct XYZ));
    }

    for (i = 0; i < A; i++)
    {
        for (j = 0; j < arr[i].numColumns; j++)
        {
            // access items
            arr[i].myRowData[j].someValue = 1;
        }
    }

    for (i = 0; i < A; i++)
    {
        free(arr[i].myRowData);
    }

    free(arr);

    return 0;
}

Solution: At last, I managed to get what I was looking for.解决方案:最后,我设法得到了我想要的东西。

Declaration:宣言:

NodeClass* currentNode;     //Single Pointer Declaration
NodeClass*** triplePtrNode; //Triple Pointer Declaration

Definition & Allocation:定义和分配:

//row, col are dynamic values at runtime

//row memory allocation
triplePtrNode = (NodeClass***)malloc(sizeof(NodeClass)*row); 
for (i=0; i<row; ++i){
    //col memory allocation
    triplePtrNode[i] = (NodeClass**)malloc(sizeof(NodeClass)*col); 
}

Usage:用法:

for (i=0; i<memberCounts; ++i){
    *(*(triplePtrNode+i)+0) = currentNode->member1;
    *(*(triplePtrNode+i)+1) = currentNode->member2;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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