C语言 char *array 和 char array[]

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20347170/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 10:25:57  来源:igfitidea点击:

char *array and char array[]

carrayspointerschar

提问by yaylitzis

if I write this

如果我写这个

 char *array = "One good thing about music";

I actually create an array? I mean it's the same like this?

我实际上创建了一个数组?我的意思是和这个一样吗?

char array[] = {"One", "good", "thing", "about", "music"};

回答by Some programmer dude

The declaration and initialization

声明和初始化

char *array = "One good thing about music";

declares a pointer arrayand make it point to a constant array of 31 characters.

声明一个指针array并使其指向一个包含 31 个字符的常量数组。

The declaration and initialization

声明和初始化

char array[] = "One, good, thing, about, music";

declares an array of characters, containing 31 characters.

声明一个字符数组,包含 31 个字符。

And yes, the size of the arrays is 31, as it includes the terminating '\0'character.

是的,数组的大小是 31,因为它包括终止'\0'字符。



Laid out in memory, it will be something like this for the first:

放在内存中,第一个是这样的:

+-------+     +------------------------------+
| array | --> | "One good thing about music" |
+-------+     +------------------------------+

And like this for the second:

第二个是这样的:

+------------------------------+
| "One good thing about music" |
+------------------------------+


Arrays decays to pointers to the first element of an array. If you have an array like

数组衰减到指向数组第一个元素的指针。如果你有一个像

char array[] = "One, good, thing, about, music";

then using plain arraywhen a pointer is expected, it's the same as &array[0].

然后array在需要指针时使用 plain ,它与&array[0].

That mean that when you, for example, pass an array as an argument to a function it will be passed as a pointer.

这意味着,例如,当您将数组作为参数传递给函数时,它将作为指针传递。

Pointers and arrays are almostinterchangeable. You can not, for example, use sizeof(pointer)because that returns the size of the actual pointer and not what it points to. Also when you do e.g. &pointeryou get the address of the pointer, but &arrayreturns a pointer to the array. It should be noted that &arrayis verydifferent from array(or its equivalent &array[0]). While both &arrayand &array[0]point to the same location, the types are different. Using the arrat above, &arrayis of type char (*)[31], while &array[0]is of type char *.

指针和数组几乎可以互换。例如,您不能使用,sizeof(pointer)因为它返回实际指针的大小而不是它指向的内容。同样,当您执行 eg 时,&pointer您会获得指针的地址,但会&array返回一个指向数组的指针。应该指出的&array是,与(或其等效物)有很大不同。虽然和 都指向相同的位置,但类型不同。使用上面的 arrat,是 type ,而是 type 。array&array[0]&array&array[0]&arraychar (*)[31]&array[0]char *



For more fun: As many knows, it's possible to use array indexing when accessing a pointer. But because arrays decays to pointers it's possible to use some pointer arithmetic with arrays.

更多乐趣:众所周知,在访问指针时可以使用数组索引。但是因为数组衰减为指针,所以可以对数组使用一些指针算法。

For example:

例如:

char array[] = "Foobar";  /* Declare an array of 7 characters */

With the above, you can access the fourth element (the 'b' character) using either

使用上述方法,您可以使用以下任一方法访问第四个元素('b' 字符)

array[3]

or

或者

*(array + 3)

And because addition is commutative, the last can also be expressed as

由于加法是可交换的,最后一个也可以表示为

*(3 + array)

which leads to the fun syntax

这导致了有趣的语法

3[array]

回答by Elias Van Ootegem

No, you're creating an array, but there's a big difference:

不,您正在创建一个数组,但有很大的不同:

char *string = "Some CONSTANT string";
printf("%c\n", string[1]);//prints o
string[1] = 'v';//INVALID!!

The array is created in a read onlypart of memory, so you can't edit the value through the pointer, whereas:

该数组是在内存的只读部分创建的,因此您无法通过指针编辑该值,而:

char string[] = "Some string";

creates the same, read only, constant string, and copiesit to the stack array. That's why:

创建相同的只读常量字符串,并将其复制到堆栈数组。这就是为什么:

string[1] = 'v';

Is valid in the latter case.
If you write:

在后一种情况下有效。
如果你写:

char string[] = {"some", " string"};

the compiler should complain, because you're constructing an array of char arrays (or char pointers), and assigning it to an array of chars. Those types don't match up. Either write:

编译器应该抱怨,因为您正在构造一个字符数组(或字符指针)数组,并将其分配给一个字符数组。这些类型不匹配。要么写:

char string[] = {'s','o','m', 'e', ' ', 's', 't','r','i','n','g', '\o'};
//this is a bit silly, because it's the same as char string[] = "some string";
//or
char *string[] = {"some", " string"};//array of pointers to CONSTANT strings
//or
char string[][10] = {"some", " string"};

Where the last version gives you an array of strings (arrays of chars) that you actually canedit...

上一个版本为您提供了一个实际上可以编辑的字符串数组(字符数组)...

回答by Piotr Zierhoffer

No. Actually it's the "same" as

不。实际上它与“相同”

char array[] = {'O', 'n', 'e', ..... 'i','c','
char array[] = {'O', 'n', 'e', ' ', /*etc*/ ' ', 'm', 'u', 's', 'i', 'c', '##代码##'};
');

Every character is a separate element, with an additional \0character as a string terminator.

每个字符都是一个单独的元素,附加一个\0字符作为字符串终止符。

I quoted "same", because there are some differences between char * arrayand char array[]. If you want to read more, take a look at C: differences between char pointer and array

我引用了“相同”,因为char * array和之间存在一些差异char array[]。如果您想阅读更多内容,请查看C: 字符指针和数组之间的差异

回答by tom

It's very similar to

它非常类似于

##代码##

but gives you read-only memory.

但给你只读内存。

For a discussion of the difference between a char[]and a char *, see comp.lang.c FAQ 1.32.

有关 achar[]和 a之间区别的讨论char *,请参阅comp.lang.c FAQ 1.32