I was trying to initialize and print array of pointers to integers. Is this method of initialization is correct or any other method is available to initialize when we declare it as array of pointers. We can also use traditional array of arrays ar1[][] anyway.
我试图初始化并打印指向整数的指针数组。这个初始化方法是正确的吗?或者当我们将它声明为指针数组时,任何其他方法都可以初始化吗?我们也可以使用传统数组ar1[][][]。
#include <stdio.h>
#include <stdlib.h>
#define NUM_COLS 4
#define NUM_ROWS 3
int main(void)
{
int rowCnt;
int colCnt;
int *ar2[NUM_ROWS]={
(int[]){11,12,13,14},
(int[]){21,22,23,24},
(int[]){31,32,33,34},
};
for(rowCnt=0;rowCnt<NUM_ROWS;rowCnt++)
{
for(colCnt=0;colCnt<NUM_COLS;colCnt++)
{
printf("%d\t",*(*(ar2+rowCnt)+colCnt));
}
printf("\n");
}
return(0);
}
3 个解决方案
#1
1
Since you know the number of rows and colums, you can define a 2D array (formally an array of arrays) instead of an array of pointers:
因为您知道行和列的数量,所以您可以定义一个2D数组(正式地说是数组数组),而不是指针数组:
int ar2[NUM_ROWS][NUM_COLS]={
{11,12,13,14},
{21,22,23,24},
{31,32,33,34},
};
#2
1
This declaration below is incorrect.
下面的声明是不正确的。
int *ar2[NUM_COLS]={
(int[]){11,12,13,14},
(int[]){21,22,23,24},
(int[]){31,32,33,34},
};
This is an array of NUM_COLS of pointers to ints. The memory of these pointers to ints do not get properly allocated this way, and will result in undefined behavior.
这是一个指向int的NUM_COLS数组。这些指向ints的指针的内存不会被正确分配,并且会导致未定义的行为。
If you wanted to allocate the entire array on the stack, you could do something like this:
如果你想在堆栈上分配整个数组,你可以这样做:
int ar2[NUM_ROWS][NUM_COLS]= {
{11,12,13,14},
{21,22,23,24},
{31,32,33,34},
{0,0,0,0}
};
If you want the pointers to ints to be properly on the heap, you should use malloc/free respectively
如果希望ints的指针正确地放在堆上,应该分别使用malloc/free
int *ar2[NUM_ROWS];
for (int i = 0; i < NUM_ROWS; ++i)
{
ar2[i] = (int*)malloc(sizeof(int) * NUM_COLS);
}
///now you can set up your arrays
memcpy(ar2[0], (int []){11,12,13,14}, 4 * sizeof(int));
memcpy(ar2[1], (int []){21,22,23,24}, 4 * sizeof(int));
memcpy(ar2[2], (int []){31,32,33,34}, 4 * sizeof(int));
///Do what you want with the array
...
///Free array once you are done
for (int i = 0; i < NUM_ROWS; ++i)
{
free(ar2[i]);
}
#3
0
This initialization is correct; compound literals have lifetime to match the block they are declared in. The literals have the same lifetime as ar2
in this case.
这个初始化是正确的;复合文本具有与声明的块匹配的生存期。在这种情况下,文字与ar2具有相同的生存期。
However I would recommend using a simple array as in dbush's answer unless you have some extra requirement (such as wanting rows of differing lengths).
但是,我建议使用一个简单的数组作为dbush的答案,除非您有一些额外的需求(比如需要不同长度的行)。
#1
1
Since you know the number of rows and colums, you can define a 2D array (formally an array of arrays) instead of an array of pointers:
因为您知道行和列的数量,所以您可以定义一个2D数组(正式地说是数组数组),而不是指针数组:
int ar2[NUM_ROWS][NUM_COLS]={
{11,12,13,14},
{21,22,23,24},
{31,32,33,34},
};
#2
1
This declaration below is incorrect.
下面的声明是不正确的。
int *ar2[NUM_COLS]={
(int[]){11,12,13,14},
(int[]){21,22,23,24},
(int[]){31,32,33,34},
};
This is an array of NUM_COLS of pointers to ints. The memory of these pointers to ints do not get properly allocated this way, and will result in undefined behavior.
这是一个指向int的NUM_COLS数组。这些指向ints的指针的内存不会被正确分配,并且会导致未定义的行为。
If you wanted to allocate the entire array on the stack, you could do something like this:
如果你想在堆栈上分配整个数组,你可以这样做:
int ar2[NUM_ROWS][NUM_COLS]= {
{11,12,13,14},
{21,22,23,24},
{31,32,33,34},
{0,0,0,0}
};
If you want the pointers to ints to be properly on the heap, you should use malloc/free respectively
如果希望ints的指针正确地放在堆上,应该分别使用malloc/free
int *ar2[NUM_ROWS];
for (int i = 0; i < NUM_ROWS; ++i)
{
ar2[i] = (int*)malloc(sizeof(int) * NUM_COLS);
}
///now you can set up your arrays
memcpy(ar2[0], (int []){11,12,13,14}, 4 * sizeof(int));
memcpy(ar2[1], (int []){21,22,23,24}, 4 * sizeof(int));
memcpy(ar2[2], (int []){31,32,33,34}, 4 * sizeof(int));
///Do what you want with the array
...
///Free array once you are done
for (int i = 0; i < NUM_ROWS; ++i)
{
free(ar2[i]);
}
#3
0
This initialization is correct; compound literals have lifetime to match the block they are declared in. The literals have the same lifetime as ar2
in this case.
这个初始化是正确的;复合文本具有与声明的块匹配的生存期。在这种情况下,文字与ar2具有相同的生存期。
However I would recommend using a simple array as in dbush's answer unless you have some extra requirement (such as wanting rows of differing lengths).
但是,我建议使用一个简单的数组作为dbush的答案,除非您有一些额外的需求(比如需要不同长度的行)。