如何声明指向int数组的指针数组
How to declare an array of pointers to int arrays?
我试图声明一个指针数组,每个指针指向不同大小的int数组。有什么想法吗?
根据您的描述,听起来您正在寻找指向指针的指针。
int **aofa;
aofa = malloc(sizeof(int*) * NUM_ARRAYS);
for (int i = 0 ; i != NUM_ARRAYS ; i++) {
aofa[i] = malloc(sizeof(int) * getNumItemsInArray(i));
}
for (int i = 0 ; i != NUM_ARRAYS ; i++) {
for (int j = 0 ; j != getNumItemsInArray(i) ; j++) {
aofa[i][j] = i + j;
}
}
NUM_ARRAYS
数组可以具有不同数量的元素,如由getNumItemsInArray(i)
函数返回的值所确定的。
int* ar[2];
int ar1[] = {1,2, 3};
int ar2[] = {5, 6, 7, 8, 9, 10};
ar[0] = ar1;
ar[1] = ar2;
cout << ar[1][2];
C版本,应该有助于澄清不同类型的声明:
#include <stdio.h>
int main()
{
/* let's make the arrays first */
int array_A[3] = {1, 2, 3};
int array_B[3] = {4, 5, 6};
int array_C[3] = {7, 8, 9};
/* now let's declare some pointers to such arrays: */
int (*pA)[3] = &array_A;
int (*pB)[3] = &array_B;
int (*pC)[3] = &array_C; /* notice the difference: */
/* int *pA[3] would be an array of 3 pointers to int because the [] operator*/
/* has a higher precedence than *(pointer) operator. so the statement would */
/* read: array_of_3 elements of type_pointer_to_int */
/* BUT, "int (*pA)[3]" is read: pointer_A (points to) type_array_of_3_ints! */
/* so now we need a different array to hold these pointers: */
/* this is called an_ARRAY_of_3_pointers to_type_array_of_3_ints */
int (*ARRAY[3])[3] = {pA, pB, pC};
/* along with a a double pointer to type_array_of_3_ints: */
int (**PTR)[3] = ARRAY;
/* and check that PTR now points to the first element of ARRAY: */
if (*PTR == pA) printf("PTR points to the first pointer from ARRAY n");
PTR++;
if (*PTR == pB) printf("PTR points to the second pointer from ARRAY! YAY!n");
return 0;
}
> $ clang prog.c -Wall -Wextra -std=gnu89 "-ansi" output:
> PTR points to the first pointer from ARRAY
> PTR points to the second pointer from ARRAY! YAY!
查看"指向对象数组的指针"一节http://www.functionx.com/cpp/Lesson24.htm它可能会对你有所帮助。
在C++中,您可以如下所示声明它。新操作符的工作方式与C中的malloc类似。
int** array = new int*[n];
#include <iostream>
using namespace std;
#define arraySize 3
const int arr1[] = {48,49,50};
const int arr2[] = {64,65,66};
const int arr3[] = {67,68,69};
typedef const int (*arrayByte);
arrayByte arrayPointer[arraySize] = {
arr1,arr2,arr3
};
void printArr(const int arr[], int size){
for(uint8_t x=0;x<size;x++){
printf("value%d=%d n",x,arr[x]);
}
}
int main()
{
printf("Print Array 0n");
printArr(arrayPointer[0],arraySize);
printf("Print Array 1n");
printArr(arrayPointer[1],arraySize);
printf("Print Array 2n");
printArr(arrayPointer[2],arraySize);
return 0;
}
试试这个代码:C++在线
相关文章:
- 如何在cpp.中使用协议缓冲区存储大缓冲区/数组(char/int)
- 按字符值访问int数组
- 将 int 数组转换为 std::vector<int*>
- 比较if语句中的数组值和int值
- C++,在int数组中输入字符串或字符会输出0,而不是ascii或error
- 在c++中获取两个大int,并将它们存储在数组中
- 声明数组>> int arr[] 时出错;在C++(虽然在 Java 中有效)?
- 有没有办法用 2D 数组 (int) 重载构造函数?
- 当大小未知时,如何迭代指向数组 (int*) 的指针?
- 接受来自函数的二维数组(int 类型)并将其返回到主函数
- 数组 + int 作为一个参数做什么
- 如何制作'bit'数组而不是数组'int'?
- 动态2d数组:int(*ptr)[4]=new int[7][4];
- 为[z][y][x]int定义数组int[x][y][z]有什么好处?
- 用Visual c++编写二维数组int[n][m]到HDF5文件
- 数组int未正确返回
- 将元素推送到动态分配的指针数组int C++
- 如何在另一个数组中插入多维数组(int而非char)
- 将数组(int*)设置为NULL是否释放其占用的内存
- C++如何将数组 int[10] push_back std::vector<int[10]>?