初始化一个指针,当字符串字面VS字符数组 [英] String literals vs array of char when initializing a pointer

查看:130
本文介绍了初始化一个指针,当字符串字面VS字符数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

href=\"http://stackoverflow.com/questions/30532077/c-char-array-and-char-pointer\">这个问题。

Inspired by this question.

我们可以用字符串初始化字符指针

We can initialize a char pointer by a string literal:

char *p = "ab";

和它是完全没有问题。
有人可能会认为这等同于以下内容:

And it is perfectly fine. One could think that it is equivalent to the following:

char *p = {'a', 'b', '\0'};

但显然并非如此。而且不仅是因为字符串存储在只读存储器,但现在看来,即使通过字符串文字具有类型字符阵列,并且初始化 {...} 字符阵列的类型,两个声明的处理方式不同,因为编译器是提出警告:

But apparently it is not the case. And not only because the string literals are stored in a read-only memory, but it appears that even through the string literal has a type of char array, and the initializer {...} has the type of char array, two declarations are handled differently, as the compiler is giving the warning:

警告:在标量初始化多余的元素

warning: excess elements in scalar initializer

在第二种情况下。什么是这种行为的解释?结果

in the second case. What is the explanation of such a behavior?

更新:

此外,在后者的情况下,指针 P 将具有 0x61 的值(第一值数组元素'A'),而不是一个内存位置,这样,编译器,作为警告,以刚刚初始化的第一个元素,并将其分配给 p

Moreover, in the latter case the pointer p will have the value of 0x61 (the value of the first array element 'a') instead of a memory location, such that the compiler, as warned, taking just the first element of the initializer and assigning it to p.

推荐答案

我觉得你很困惑,因为的char * p =AB; 字符p [] =AB; 也有类似的语义,但含义不同。

I think you're confused because char *p = "ab"; and char p[] = "ab"; have similar semantics, but different meanings.

我认为,后一种情况下(字符P [] =AB; )最好被视为一个的缩写符号的对字符p [] = {'A​​','b','\\ 0'}; (初始化由初始确定的大小的数组)。其实,在这种情况下,你可以说AB是不是真的作为的字符串

I believe that the latter case (char p[] = "ab";) is best regarded as a short-hand notation for char p[] = {'a', 'b', '\0'}; (initializes an array with the size determined by the initializer). Actually, in this case, you could say "ab" is not really used as a string literal.

然而,在前一种情况下(的char * p =AB; )是因为它只是初始化指针不同的 P 指向的只读字符串AB

However, the former case (char *p = "ab";) is different in that it simply initializes the pointer p to point to the first element of the read-only string literal "ab".

我希望你能看到其中的差别。而字符P [] =AB; 重新presentable作为初始化,如你所说,的char * p =AB ; 不是,因为指针是,嗯,不是数组,并使用数组初始化初始化它们确实完全不同的东西(也就是给他们的第一个元素的值, 0x61 你的情况)。

I hope you see the difference. While char p[] = "ab"; is representable as an initialization such as you described, char *p = "ab"; is not, as pointers are, well, not arrays, and initializing them with an array initializer does something entirely different (namely give them the value of the first element, 0x61 in your case).

长话短说,只有C编译器替换字符串文字以字符数组初始化它是否适合这样做,也就是说,它被用来初始化字符阵列。

Long story short, C compilers only "replace" a string literal with a char array initializer if it is suitable to do so, i.e. it is being used to initialize a char array.

这篇关于初始化一个指针,当字符串字面VS字符数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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