C-动态数组

发布于 2024-10-20 19:40:31 字数 781 浏览 4 评论 0原文

我不太明白指针如何与 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夜访吸血鬼 2024-10-27 19:40:31

sizeof(array) 是指针的大小,它恰好是您平台上 int 大小的两倍。

C 中没有办法获取数组的长度。你只能自己记住它。

sizeof(array) is the size of a pointer, which just happens to be the twice the size of an int on your platform.

There's no way to get the length of an array in C. You just have to remember it yourself.

〃温暖了心ぐ 2024-10-27 19:40:31

首先,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 be sizeof(int *)

Next, sizeof(array) / sizeof(int) corresponds to sizeof(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.

守护在此方 2024-10-27 19:40:31

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.

葬心 2024-10-27 19:40:31

首先,arrayThree的分配应该是

int **arrayThree = malloc(2 * sizeof *arrayThree);

由于arrayThree的类型是int **,那么*arrayThree的类型int *

sizeof (array) / sizeof (int) 未返回您期望的结果的原因是 array 是一个指针(类型 int *),不是数组类型,因此 sizeof 返回指针 object 本身包含的字节数,而不是指向的元素数。

仅从指针值无法获知有多少个元素被指向;您必须单独跟踪该信息。

First of all, the allocation of arrayThree should be

int **arrayThree = malloc(2 * sizeof *arrayThree);

Since the type of arrayThree is int **, then the type of *arrayThree is int *.

The reason that sizeof (array) / sizeof (int) isn't returning what you expect is that array is a pointer (type int *), not an array type, so sizeof 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文