【问题标题】:Retrieve value from array inside of another array [C]从另一个数组中的数组中检索值 [C]
【发布时间】:2017-01-23 17:51:11
【问题描述】:

看看这个数组:

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); 

希望这对将来的人有所帮助。

【问题讨论】:

  • h002代表你的数组的地址,而*hoo2h002[0]代表存储在h002指向的地址的值。
  • 看来我当时搞混了。如果是这种情况,是否有理由添加指针使我的程序无法编译?
  • 可能是因为 c000 不是int 数组,而是int * 数组;尝试将其声明为const int c000[64][1][3]
  • 我试过了,还是不行。
  • @m.raynal "h002 代表您的阵列的地址" - 不!它表示int 的一维数组。数组不是指针!

标签: c arrays multidimensional-array int


【解决方案1】:

我认为基本的误解是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的第一个元素。

【讨论】:

  • 这意味着我应该能够引用 h002 (&h002) 并获得它的第一个元素,对吗?我这样做了,还是不行,还是返回h002的地址。
  • 如果您引用某些内容,例如通过写&h002,你将永远得到一个地址,而不是一个值。如果要访问数组的值,请使用索引,例如myValue = h002[0];如果您想访问您持有指针的值,请取消引用指针,例如myValue = *ptrToH002 - 注意ptrToH002 指向数组h002 的第一个元素。好吗?
  • 好的,我明白了。因此,假设我的 c001 数组与我原来的帖子中的相同,但我将 h002 作为 h002[0]。这编译得很好,但是我仍然得到一个地址,而不是元素。然后呢?
  • 让你的代码在你的帖子中,然后尝试int myVar = *(c000[0][0][0]) - 这应该会给你1
  • 是的!这正是我一直在寻找的!哦,伙计,非常感谢!
【解决方案2】:

多维数组示例:

int main(){
    int a1[] = {1,2,3,4};
    int a2[] = {5,6,7,8};
    int a3[] = {9,10,11,12};
    int * superArr[3] = {a1,a2,a3};
    printf("%d\n", superArr[2][1]);
}

您应该能够使用此示例将代码更改为您需要的内容。

【讨论】:

  • 首先,这是标记为c,而不是c++。其次,这不是一个多维数组。
  • 现在是 C,谢谢。你会发现二维其实是多维的。
  • 它仍然是多维的,只是不能保证在内存中彼此相邻或强制具有与 int [][] ={{ ... }} 相同的 col 大小。
  • 指针不是数组。 @EOF 是对的:这不是多维数组,而是完全不同的数据结构。仅仅因为您可以同时使用索引运算符并不能使它们相同甚至相似。
  • 多维数组的定义是数组的数组。这是一个指向数组的指针数组。
猜你喜欢
  • 1970-01-01
  • 2022-11-10
  • 2020-08-22
  • 2015-03-24
  • 1970-01-01
  • 2021-10-12
  • 1970-01-01
  • 2020-04-06
  • 1970-01-01
相关资源
最近更新 更多