方括号数组和指针数组有什么区别?
作为一名非 C/C++ 专家,我一直认为方括号和指针数组是相等的.
As a non-C/C++ expert I always considered square brackets and pointers arrays as equal.
即:
char *my_array_star;
char my_array_square[];
但我注意到在结构/类中使用时,它们的行为不一样:
But I noticed that when use in a structure/class they don't behave the same :
typedef struct {
char whatever;
char *my_array_star;
} my_struct_star;
typedef struct {
char whatever;
char my_array_square[];
} my_struct_square;
下面一行显示 16,whatever
占用 1 个字节,my_array_pointer
占用 8 个字节.由于填充,总结构大小为 16.
The line below displays 16, whatever
takes 1 byte, my_array_pointer
takes 8 bytes.
Due to the padding the total structure size is 16.
printf("my_struct_star: %li\n",sizeof(my_struct_star));
下一行显示 1,whatever
占用 1 个字节,my_array_pointer
不考虑在内.
The line below displays 1, whatever
takes 1 byte, my_array_pointer
isn't taken in account.
printf("my_struct_square: %li\n",sizeof(my_struct_square));
通过玩弄我注意到方括号被用作结构中的额外空间
By playing around I noticed that square brackets are used as extra space in the structure
my_struct_square *i=malloc(2);
i->whatever='A';
i->my_array_square[0]='B';
线吹显示A:
printf("i[0]=%c\n",((char*)i)[0]);
线吹显示B:
printf("i[1]=%c\n",((char*)i)[1]);
所以我不能再说方括号等于指针了.但我想了解这种行为的原因.我害怕错过这些语言的一个关键概念.
So I cannot say anymore that square brackets are equals to pointers. But I'd like to understand the reason of that behavior. I'm afraid of missing a key concept of that languages.
数组和指针的行为不同,因为它们不完全相同,看起来就是这样.
Arrays and pointers don't behave the same because they're not the same at all, it just seems that way.
数组是一组连续的项目,而指针是......好吧......一个指向单个项目的指针.
Arrays are a group of contiguous items while a pointer is ... well ... a pointer to a single item.
被指向的单个项目很可能是数组中的第一个项目,因此您也可以访问其他项目,但指针本身既不知道也不关心这一点.
That single item being pointed to may well be the first in an array so that you can access the others as well, but the pointer itself neither knows nor cares about that.
数组和指针通常看起来相同的原因是,在许多情况下,数组会衰减为指向该数组第一个元素的指针.
The reason that arrays and pointers often seem to be identical is that, in many cases, an array will decay to a pointer to the first element of that array.
发生这种情况的地方之一是在函数调用中.当您将数组传递给函数时,它会衰减为指针.这就是为什么诸如数组大小之类的东西不会显式传递给函数的原因.我的意思是:
One of the places this happens is in function calls. When you pass an array to a function, it decays into a pointer. That's why things like the size of an array don't pass through to the function explicitly. By that I mean:
#include <stdio.h>
static void fn (char plugh[]) {
printf ("size = %d\n", sizeof(plugh)); // will give char* size (4 for me).
}
int main (void) {
char xyzzy[10];
printf ("size = %d\n", sizeof(xyzzy)); // will give 10.
fn (xyzzy);
return 0;
}
您会发现的另一件事是,虽然您可以 plugh++
和 plugh--
满足您的内心需求(只要您不在外部取消引用)数组),你不能用数组 xyzzy
做到这一点.
The other thing you'll find is that, while you can plugh++
and plugh--
to your hearts content (as long as you don't dereference outside of the array), you can't do that with the array xyzzy
.
在您的两种结构中,存在重大差异.在指针版本中,您在结构内部有一个固定大小的指针,它将指向结构的外部项.
In your two structures, there's a major difference. In the pointer version, you have a fixed size pointer inside the structure, which will point to an item outside of the structure.
这就是它占用空间的原因 - 您的 8 字节指针与 8 字节边界对齐,如下所示:
That's why it takes up space - your 8-byte pointer is aligned to an 8-byte boundary as follows:
+----------------+
| 1 char variable|
+----------------+
| 7 char padding |
+----------------+
| 8 char pointer |
+----------------+
使用无界"数组,您可以将它放在结构中,您可以将其设置为您想要的大小——您只需要在创建变量时分配足够的内存.默认情况下(即根据sizeof
),大小为零:
With the "unbounded" array, you have it inside the structure and you can make it as big as you want - you just have to allocate enough memory when you create the variable. By default (ie, according to the sizeof
), the size is zero:
+----------------+
| 1 char variable|
+----------------+
| 0 char array |
+----------------+
但是你可以分配更多的空间,例如:
But you can allocate more space, for example:
typedef struct {
char whatever;
char my_array_square[];
} my_struct_square;
my_struct_square twisty = malloc (sizeof (my_struct_square) + 10);
给你一个变量 twisty
,它有一个 whatever
字符和一个名为 my_array_square
的十个字符的数组.
gives you a variable twisty
which has a whatever
character and an array of ten characters called my_array_square
.
这些无界数组只能出现在结构的末尾并且只能有一个(否则编译器将不知道这些可变长度部分的开始和结束位置)并且它们专门允许任意大小的数组位于结构结束.
These unbounded arrays can only appear at the end of a structure and there can be only one (otherwise the compiler would have no idea where these variable length section began and ended) and they're specifically to allow arbitrarily sized arrays at the end of structures.