看一下这个数组:

const int *c000[64][1][3] =
{
//Row 1
{ {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} },

//Row 2
{ {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} },

//Row 3
{ {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} },

//Row 4
{ {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} },

//Row 5
{ {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} },

//Row 6*
{ {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} },

//Row 7
{ {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} },

//Row 8
{ {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} },
};


忽略数组的奇怪大小和结构,这并不重要。我需要能够在该数组中使用一个数组。例如,我有一个名为h002的数组:

const int h002[18] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 0xe0, 0xe0};           //01


我需要能够从c000内部使用h002。如:

{ {h002, 0, 0} },


这样编译就可以了

myVar = c000[0][0][0];


检索h002的地址。我需要h002中的第一项,而不是地址。我很困惑为什么它甚至可以给我h002的地址?我可以想象* h002会检索地址,但是那根本无法编译。

我在SO上看到了几个非常类似于我的问题,例如:

Creating an array of int arrays in C?

我已经尝试了上面的特定示例。当我将它保存在main.c文件中时,它可以工作,但是当我在包含c000和h002的同一.c文件中尝试该文件时,它将无法编译。也许这与它有关?我不确定为什么会这样,考虑在c000中使用h002返回h002的地址就好了。奇怪的是,以上链接中显示的代码无法在我的主文件之外进行编译。

我觉得自己犯了一些显而易见的小错误。现在,我已经断断续续地花了大约5天的时间。反复试验和研究使我无处可去。研究非常困难,因为使用这样的数组似乎并没有太多的事情,而且我的发现都对我没有明显的帮助。

随意问的问题。如果需要,我很乐意指定更多信息。

编辑:非常感谢斯蒂芬·莱希纳(Stephan Lechner)帮助我解决了我的问题。为了获得所需的结果,我必须做

myVar = *(c000[0][0][0]);


这很完美。我还可以在末尾添加任意数字,以检索h002中的不同索引。例如:

myVar = *(c000[0][0][0] + 7);


希望这对以后的人有所帮助。

最佳答案

我认为基本的误解是const int *c000[64][1][3]的含义,它表示一个指向int的三维数组,而不是一个指向3D整数数组的指针。为了说明这一点,请考虑以下简化示例以及编译器警告:

int aSingleIntValue = 10;  // integer value
int *aSinglePtrToIntValue = &aSingleIntValue;  // pointer to an integer value

// 1D-array of integer values
int oneDArrayOfInt[5] = { 3,4,5,6,7 };

// 1D-array of pointers to int; Note: OK, since 0 is interpreted as NULL
int *oneDArrayOfIntPtrOK[5] = { aSinglePtrToIntValue,0,0,0,0 };

// 1D-array of pointers to int; Note: 3,4,.. are int values, not pointers to int; Hence compiler complains:
// Warning: Incompatible integer to pointer conversion initializing "int *"  with an expression of type "int"
int *oneDArrayOfIntPtrWarning[5] = { 3,4,5,6,7 };

// 3D-array of pointers to int; Note: OK, since 0 is interpreted as NULL
int *threeDArrayOfIntPtrOK[5][5][5] = { { { aSinglePtrToIntValue,0,0 }, { 0,0,0} } };

// 3D-array of pointers to int; Warining: Incompatible integer to pointer conversion initializing "int *"  with an expression of type "int"
int *threeDArrayOfIntPtrWarning[5][5][5] = { { { 3,4,5 }, { 2,3,1} } };


只是为了显示另一种含义:如果要声明一个指向5个整数的数组的指针,则可以表示为:

typedef int arrayOf5Int_t[5];
arrayOf5Int_t *arrayOf5IntPtr = &oneDArrayOfInt;


鉴于此,让我们解释一下

const int h002[18] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 0xe0, 0xe0};
const int *c000[64][1][3] = { { { h002, 0, 0 } } };


请注意,c000的元素是指向整数的指针,并且请注意,在数组初始化程序的上下文中使用的变量h002会衰减为指向整数的指针(指向h002的第一个元素)。这与声明const int* ptrToH002 = h002几乎相同,并且以下打印输出表明这实际上是相同的:

const int *elemAt0x0x0 = c000[0][0][0];
const int *ptrToH002 = h002;
int isTheSame = elemAt0x0x0 == ptrToH002;
printf("pointer elemAt0x0x0 == ptrToH002 is %s", (isTheSame ? "true" : "false"));
// Prints: pointer elemAt0x0x0 == ptrToH002 is true


因此,应该清楚myVar = c000[0][0][0]检索地址,而不是整数值或整数数组。地址指向数组h002的第一个元素。

关于c - 从另一个数组内部的数组中检索值[C],我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41812556/

10-11 22:05
查看更多