简体   繁体   English

char *数组和char数组[]

[英]char *array and char array[]

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"};

The declaration and initialization 声明和初始化

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

declares a pointer array and 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 array when a pointer is expected, it's the same as &array[0] . 然后在预期指针时使用plain array ,它与&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 almost interchangeable. 指针和数组几乎可以互换。 You can not, for example, use sizeof(pointer) because that returns the size of the actual pointer and not what it points to. 例如,您不能使用sizeof(pointer)因为它返回实际指针的大小而不是它指向的大小。 Also when you do eg &pointer you get the address of the pointer, but &array returns a pointer to the array. 此外,当你执行eg &pointer你得到了&pointer的地址,但是&array返回一个指向&array的指针。 It should be noted that &array is very different from array (or its equivalent &array[0] ). 应该注意的是&arrayarray (或它的等价&array[0]非常不同。 While both &array and &array[0] point to the same location, the types are different. 虽然&array&array[0]指向同一位置,但类型不同。 Using the arrat above, &array is of type char (*)[31] , while &array[0] is of type char * . 使用上面的arrat, &array的类型为char (*)[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]

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 only part 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 copies it 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. 编译器应该抱怨,因为你正在构造一个char数组(或char指针)数组,并将它分配给一个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 can edit... 最后一个版本为您提供了一个实际可以编辑的字符串数组(字符数组)...

No. Actually it's the "same" as 不。实际上它与“相同”

char array[] = {'O', 'n', 'e', ..... 'i','c','\0');

Every character is a separate element, with an additional \\0 character as a string terminator. 每个字符都是一个单独的元素,附加的\\0字符作为字符串终止符。

I quoted "same", because there are some differences between char * array and char array[] . 我引用“相同”,因为char * arraychar array[]之间存在一些差异。 If you want to read more, take a look at C: differences between char pointer and array 如果您想阅读更多内容,请查看C:char指针和数组之间的差异

It's very similar to 它非常相似

char array[] = {'O', 'n', 'e', ' ', /*etc*/ ' ', 'm', 'u', 's', 'i', 'c', '\0'};

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 . 有关char[]char *之间区别的讨论,请参阅comp.lang.c FAQ 1.32

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM