简体   繁体   English

char数组和char指针的比较

[英]Comparison of char array and char pointer

While playing around I found a result I cannot get my head around, which involves char arrays and pointer. 在玩的时候,我发现了一个结果,我无法理解,这涉及到char数组和指针。

char charArray[] = "Array";
char* charPtr1 = "Array";
char* charPtr2 = "Array";

why is charArray != charPtr1/2 , but charPtr 1 == charPtr2 ? 为什么是charArray != charPtr1/2 ,但是charPtr 1 == charPtr2

I though when creating charPtr1 , it would create a temp array and point to there. 我在创建charPtr1时会创建一个临时数组并指向那里。 If that's the case, why aren't they the same? 如果是这样的话,为什么他们不一样?

 char charArray[] = "Array"; char* charPtr1 = "Array"; char* charPtr2 = "Array"; 

why is charArray != charPtr1/2, but charPtr 1 == charPtr2? 为什么是charArray!= charPtr1 / 2,但是charPtr 1 == charPtr2?

charArray is in fact char charArray[6] = { 'A', 'r', 'r', 'a', 'y', 0 }; charArray实际上是char charArray[6] = { 'A', 'r', 'r', 'a', 'y', 0 }; , so it is an array, whose contains can be changed ,所以它是一个数组,其包含可以更改

charPtr1 and charPtr2 are pointer to a char so none of them can be equal to charArray (except after charPtr1 = charArray; etc of course) charPtr1charPtr2是指向char指针,因此它们都不能等于charArray (除了charPtr1 = charArray;当然等)

The fact charPtr1 and charPtr2 is an optimization of the compiler, that one detect the literal string "Array" is used several times, defines it one time and use its address to initialize the two variables 事实charPtr1charPtr2是编译器的优化,一个检测文字字符串“Array”多次使用,定义一次并使用其地址初始化两个变量

This might help. 可能有所帮助。

A disassembly of 拆卸

char charArray1[] = "Array";
char* charPtr1 = "Array";
char* charPtr2 = "Array";

with GCC8.3 shows 用GCC8.3显示

charArray1:
        .string "Array"
.LC0:
        .string "Array"
charPtr1:
        .quad   .LC0
charPtr2:
        .quad   .LC0

In other words, the two pointers point to the same memory location containing the string "Array", while the array holds its own copy of the string. 换句话说,两个指针指向包含字符串“Array”的相同内存位置,而该数组包含其自己的字符串副本。

As the link suggests, the memory for the char array is separated like that due to the different types in question. 正如链接所示,由于所讨论的类型不同,char数组的内存被分开。 Regarding the pointers, because their job is to just point to some data, probably the compiler chooses to optimize out duplicated allocations for the same literal data. 关于指针,因为他们的工作只是指向一些数据,可能编译器选择优化相同文字数据的重复分配。

The literal data for the pointers is read-only. 指针的文字数据是只读的。

The first char charArray[] = "Array"; 第一个char charArray[] = "Array"; is equivalent to char charArray[] = {'A', 'r', 'r', 'a', 'y', '\\0'} which is an initialization of the array object with automatic storage duration. 相当于char charArray[] = {'A', 'r', 'r', 'a', 'y', '\\0'}这是具有自动存储持续时间的数组对象的初始化。

The cases 2, 3 both points to the first element of the same array object meaning the pointers compare equal. 情况2,3都指向同一数组对象的第一个元素,这意味着指针比较相等。 The standard specifies that 6.5.2.5(p7) : 该标准规定6.5.2.5(p7)

String literals, and compound literals with const-qualified types, need not designate distinct objects 字符串文字和具有const限定类型的复合文字不需要指定不同的对象

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

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