是什么方括号数组和指针数组之间的区别? [英] What is the difference between square bracket arrays and pointer arrays?

查看:156
本文介绍了是什么方括号数组和指针数组之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为一个非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行,任何需要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行,任何需要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';

该行一击显示答:

the line blow displays A:

printf("i[0]=%c\n",((char*)i)[0]);

行显示的打击A:

the line blow displays 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.

在此先感谢,
最好的问候各位

Thanks in advance, Best regards to all of you

推荐答案

数组和指针不规矩一样。

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

给你一个变量曲折具有任何字符和十个字符数组名为 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.

这篇关于是什么方括号数组和指针数组之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆