C-动态数组
我不太明白指针如何与 C 数组一起使用。这是我得到的一些代码:
int arrayOne[] = {1, 2, 3};
int arrayTwo[] = {4, 5, 6, 7};
int **arrayThree = (int **)malloc(2 * sizeof(int));
arrayThree[0] = arrayOne;
arrayThree[1] = arrayTwo;
for (int i = 0; i < 2; i++) {
int *array = arrayThree[i];
int length = sizeof(array) / sizeof(int);
for (int j = 0; j < length; j++)
printf("arrayThree[%d][%d] = %d\n", i, j, array[j]);
}
我本来期望它输出以下内容:
arrayThree[0][0] = 1
arrayThree[0][1] = 2
arrayThree[0][2] = 3
arrayThree[1][0] = 4
arrayThree[1][1] = 5
arrayThree[1][2] = 6
arrayThree[1][3] = 7
它实际打印出来的是:
arrayThree[0][0] = 1
arrayThree[0][1] = 2
arrayThree[1][0] = 4
arrayThree[1][1] = 5
为什么?!
I don't quite understand how pointers work with C arrays. Here's some code I got:
int arrayOne[] = {1, 2, 3};
int arrayTwo[] = {4, 5, 6, 7};
int **arrayThree = (int **)malloc(2 * sizeof(int));
arrayThree[0] = arrayOne;
arrayThree[1] = arrayTwo;
for (int i = 0; i < 2; i++) {
int *array = arrayThree[i];
int length = sizeof(array) / sizeof(int);
for (int j = 0; j < length; j++)
printf("arrayThree[%d][%d] = %d\n", i, j, array[j]);
}
I would have expected this to output the following:
arrayThree[0][0] = 1
arrayThree[0][1] = 2
arrayThree[0][2] = 3
arrayThree[1][0] = 4
arrayThree[1][1] = 5
arrayThree[1][2] = 6
arrayThree[1][3] = 7
What it actually prints out is:
arrayThree[0][0] = 1
arrayThree[0][1] = 2
arrayThree[1][0] = 4
arrayThree[1][1] = 5
Why?!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
sizeof(array)
是指针的大小,它恰好是您平台上int
大小的两倍。C 中没有办法获取数组的长度。你只能自己记住它。
sizeof(array)
is the size of a pointer, which just happens to be the twice the size of anint
on your platform.There's no way to get the length of an array in C. You just have to remember it yourself.
首先,
int **arrayThree = (int **)malloc(2 * sizeof(int))
是错误的,应该是sizeof(int *)
接下来,
sizeof(array) / sizeof(int)
对应于sizeof(int *) / sizeof(int)
这不是你想要的。您将其视为数组的指针中没有“嵌入”大小信息,您需要手动管理大小。
First of all,
int **arrayThree = (int **)malloc(2 * sizeof(int))
is wrong, it should besizeof(int *)
Next,
sizeof(array) / sizeof(int)
corresponds tosizeof(int *) / sizeof(int)
which is not what you want.There is no "embedded" size information in a pointer that you treat as an array, you will need to manually manage the size.
C 中没有内置机制来跟踪数组的大小,您需要自己维护它,并将其传递给任何将数组作为参数的函数。
但是,如果您确实需要在 C 中以动态方式广泛使用数组,您可以在 C 中构建自己的动态数组库,而无需付出太多努力。有关更多信息,请参阅以下教程:goo.gl/vYhkF。
There is no built-in mechanism to keep track of the size of an array in C, you need to maintain it yourself, and pass it to any function that takes your array as parameter.
However, if you really need to use arrays extensively and in a dynamic manner in C, you can build your own library of dynamic arrays in C, without too much effort. For more information, refer to the following tutorial: goo.gl/vYhkF.
首先,
arrayThree
的分配应该是由于
arrayThree
的类型是int **
,那么*arrayThree的类型
是int *
。sizeof (array) / sizeof (int)
未返回您期望的结果的原因是array
是一个指针(类型int *
),不是数组类型,因此sizeof
返回指针 object 本身包含的字节数,而不是指向的元素数。仅从指针值无法获知有多少个元素被指向;您必须单独跟踪该信息。
First of all, the allocation of
arrayThree
should beSince the type of
arrayThree
isint **
, then the type of*arrayThree
isint *
.The reason that
sizeof (array) / sizeof (int)
isn't returning what you expect is thatarray
is a pointer (typeint *
), not an array type, sosizeof
returns the number of bytes contained in the pointer object itself, not the number of elements pointed to.There is no way to know from the pointer value alone how many elements are being pointed to; you must keep track of that information separately.