如何在一个分配 C 中动态分配二维数组
How can I dynamically allocate 2D-array in one allocate C
你能帮我弄清楚如何在一次分配调用中分配一个二维数组吗?
我试过:
int** arr = (int**)malloc(num * num * sizeof(int*));
但它不起作用。
num
是行和列。
您可以通过两种方式之一分配二维数组。
1:指向数组的指针数组
这将是:
int rows = 10;
int cols = 10;
int **array = malloc(rows * sizeof(int*));
for (int i = 0; i < rows; i++) {
array[i] = malloc(cols * sizeof(int));
}
array
现在将指向一个指针列表,每个指针代表一行,这些指针将指向该行中的元素。在这种情况下,您可以使用 array[n][m]
访问第 n 行和第 m 列
2:单个连续块
这可能是您想要的方法,您可以在一次分配中完成所有操作。这将要求您以一维表示形式存储二维数组。
int rows = 10;
int cols = 10;
int *array = malloc(rows * cols * sizeof(int));
然后您可以使用偏移量存储和检索第 n 行和第 m 列:array[(n * cols) + m]
How can i to allocate dynamically array2D in 1 allocate C
让我们先从什么是二维数组说起:
2D array or "array 3 of array 4 of int"
示例
int arr1[3][4];
arr1[0][0] = this;
OP 的代码声明了一个 pointer to pointer to int,不是二维数组,也不是指向二维数组的指针。
顺便说一句,不需要演员表。
int** arr = (int**)malloc(num * num * sizeof(int*));
代码可以为二维数组分配内存,return指向该内存的指针。 pointer to array 5 of array 6 of int
int (*arr2)[5][6] = malloc(sizeof *arr2);
if (arr2 == NULL) return EXIT_FAILURE;
(*arr2)[0][0] = this;
return EXIT_SUCCESS;
// or with Variable Length Arrays in C99 and optionally in C11
int (*arr3)[num][num] = malloc(sizeof *arr3);
(*arr3)[0][0] = that;
或者,代码可以为一维数组分配内存,return 指向该内存的指针。 pointer to array 8 of int。有时这通常是人们想要的 "allocate 2D" 数组,实际上是指向一维数组的指针
int (*arr4)[8] = malloc(sizeof *arr4 * 7);
arr4[0][0] = this;
// or
int (*arr5)[num] = malloc(sizeof *arr5 * num);
arr5[0][0] = that;
尽管我认为“二维整数数组”的含义明确类似于 int arr[10][10]
,但在网络上搜索得出了诸如 "using an array of pointers" 或 "using a pointer to a pointer" 之类的解释(cf,例如,this post)。该答案的其余部分基于 int arr[r][c]
形式的二维数组,其中 r
表示行数,c
表示每行的列数。
如果不支持可变长度数组,则至少 c
必须是 const 表达式(即在编译时已知)。相反,r
也可以在运行时定义,这样至少行数是 "dynamic"。然后可以将二维数组表示为一维数组的(可能不完整的)数组:
#define COLS 3
void printArray(int array[][COLS], int rows) {
for(int row=0; row<rows; row++) {
for (int col=0; col<COLS; col++) {
printf("%d ", array[row][col]);
}
printf("\n");
}
}
int main() {
typedef int oneD[COLS];
int rows = 5;
size_t myArray5RowsSize = rows*sizeof(oneD);
oneD *myArray5Rows = malloc(myArray5RowsSize);
memset(myArray5Rows,0,myArray5RowsSize);
myArray5Rows[0][0] = 0;
myArray5Rows[1][1] = 1;
myArray5Rows[2][2] = 2;
printArray(myArray5Rows, 5);
return 0;
}
你能帮我弄清楚如何在一次分配调用中分配一个二维数组吗?
我试过:
int** arr = (int**)malloc(num * num * sizeof(int*));
但它不起作用。
num
是行和列。
您可以通过两种方式之一分配二维数组。
1:指向数组的指针数组
这将是:
int rows = 10;
int cols = 10;
int **array = malloc(rows * sizeof(int*));
for (int i = 0; i < rows; i++) {
array[i] = malloc(cols * sizeof(int));
}
array
现在将指向一个指针列表,每个指针代表一行,这些指针将指向该行中的元素。在这种情况下,您可以使用 array[n][m]
2:单个连续块
这可能是您想要的方法,您可以在一次分配中完成所有操作。这将要求您以一维表示形式存储二维数组。
int rows = 10;
int cols = 10;
int *array = malloc(rows * cols * sizeof(int));
然后您可以使用偏移量存储和检索第 n 行和第 m 列:array[(n * cols) + m]
How can i to allocate dynamically array2D in 1 allocate C
让我们先从什么是二维数组说起:
2D array or "array 3 of array 4 of int"
int arr1[3][4];
arr1[0][0] = this;
OP 的代码声明了一个 pointer to pointer to int,不是二维数组,也不是指向二维数组的指针。
顺便说一句,不需要演员表。
int** arr = (int**)malloc(num * num * sizeof(int*));
代码可以为二维数组分配内存,return指向该内存的指针。 pointer to array 5 of array 6 of int
int (*arr2)[5][6] = malloc(sizeof *arr2);
if (arr2 == NULL) return EXIT_FAILURE;
(*arr2)[0][0] = this;
return EXIT_SUCCESS;
// or with Variable Length Arrays in C99 and optionally in C11
int (*arr3)[num][num] = malloc(sizeof *arr3);
(*arr3)[0][0] = that;
或者,代码可以为一维数组分配内存,return 指向该内存的指针。 pointer to array 8 of int。有时这通常是人们想要的 "allocate 2D" 数组,实际上是指向一维数组的指针
int (*arr4)[8] = malloc(sizeof *arr4 * 7);
arr4[0][0] = this;
// or
int (*arr5)[num] = malloc(sizeof *arr5 * num);
arr5[0][0] = that;
尽管我认为“二维整数数组”的含义明确类似于 int arr[10][10]
,但在网络上搜索得出了诸如 "using an array of pointers" 或 "using a pointer to a pointer" 之类的解释(cf,例如,this post)。该答案的其余部分基于 int arr[r][c]
形式的二维数组,其中 r
表示行数,c
表示每行的列数。
如果不支持可变长度数组,则至少 c
必须是 const 表达式(即在编译时已知)。相反,r
也可以在运行时定义,这样至少行数是 "dynamic"。然后可以将二维数组表示为一维数组的(可能不完整的)数组:
#define COLS 3
void printArray(int array[][COLS], int rows) {
for(int row=0; row<rows; row++) {
for (int col=0; col<COLS; col++) {
printf("%d ", array[row][col]);
}
printf("\n");
}
}
int main() {
typedef int oneD[COLS];
int rows = 5;
size_t myArray5RowsSize = rows*sizeof(oneD);
oneD *myArray5Rows = malloc(myArray5RowsSize);
memset(myArray5Rows,0,myArray5RowsSize);
myArray5Rows[0][0] = 0;
myArray5Rows[1][1] = 1;
myArray5Rows[2][2] = 2;
printArray(myArray5Rows, 5);
return 0;
}