指向2D数组的指针数组

时间:2017-11-22 18:25:57

标签: c pointers multidimensional-array

将一组指针数组保存到变量的一组2D数组中的正确方法是什么?

const int arr0[][4] = {{10, 11, 12, 13},    \
                       {14, 15, 16, 17},    \
                       {18, 19, 20, 21},    \
                       {22, 23, 24, 25}};
const int arr1[][4] = {{80, 81, 82, 83},    \
                       {84, 85, 86, 87},    \
                       {88, 89, 90, 91},    \
                       {92, 93, 94, 95}};

int *ptr[2];     // Elements of ptr should point to any array size
ptr[0] = arr0;   // this give Warning C4047 '=': 'bool *' differs in levels of indirection from 'const bool (*)[4]'

我在哪里想念* /& /()??

由于

编辑: 由于我不知道在构建时间arr0 / 1的实际大小(可能会有所不同),我想保持两个指针(*ptr[2] ?)引用arr0 / arr1并使用一些指针算法访问它们的元素,类似于(假设我有动态存储的宽度= 4:

int out_2_3 = *(ptr[0] + 2*width + 3) + *(ptr[1] + 2*width + 3);

//期望:20 + 90

0 个答案:

没有答案
相关问题