C语言 char a[]="string"; 之间的区别 字符 *p="字符串";

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2938895/
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 05:32:00  来源:igfitidea点击:

Difference between char a[]="string"; char *p="string";

c

提问by Blue Sky

Possible Duplicates:
What is the difference between char s[] and char *s in C?

可能的重复:
C 中的 char s[] 和 char *s 有什么区别?

What is the difference between char a[]="string";and char *p="string";?

char a[]="string";和 和有char *p="string";什么区别?

回答by Incognito

The first one is array the other is pointer.

第一个是数组,另一个是指针。

The array declaration "char a[6];" requests that space for six characters be set aside, to be known by the name "a." That is, there is a location named "a" at which six characters can sit. The pointer declaration "char *p;" on the other hand, requests a place which holds a pointer. The pointer is to be known by the name "p," and can point to any char (or contiguous array of chars) anywhere.

数组声明“ char a[6];”要求留出六个字符的空间,以名称“ a.”表示,即有一个名为“ a”的位置可以放置六个字符。char *p;另一方面,指针声明“ ”请求一个存放指针的地方。指针的名称为“ p,”,可以指向任何地方的任何字符(或连续的字符数组)。

The statements

声明

char a[] = "hello";
char *p = "world";

would result in data structures which could be represented like this:

将导致可以这样表示的数据结构:

   +---+---+---+---+---+---+
a: | h | e | l | l | o |##代码## |
   +---+---+---+---+---+---+
   +-----+     +---+---+---+---+---+---+
p: |  *======> | w | o | r | l | d |##代码## |
   +-----+     +---+---+---+---+---+---+

It is important to realize that a reference like x[3] generates different code depending on whether x is an array or a pointer. Given the declarations above, when the compiler sees the expression a[3], it emits code to start at the location "a," move three past it, and fetch the character there. When it sees the expression p[3], it emits code to start at the location "p," fetch the pointer value there, add three to the pointer, and finally fetch the character pointed to. In the example above, both a[3] and p[3] happen to be the character 'l', but the compiler gets there differently.

重要的是要意识到像 x[3] 这样的引用会根据 x 是数组还是指针生成不同的代码。给定上面的声明,当编译器看到表达式 a[3] 时,它会发出代码以从位置“a”开始,移动三个经过它,并在那里获取字符。当它看到表达式 p[3] 时,它发出代码以从位置“p”开始,在那里获取指针值,在指针上加三,最后获取指向的字符。在上面的例子中,a[3] 和 p[3] 碰巧都是字符 'l',但编译器得到的结果不同。

You can use search there are tons of explanations on the subject in th internet.

您可以使用搜索,互联网上有大量关于该主题的解释。

回答by Prasoon Saurav

char a[]="string";//ais an array of characters.

char a[]="string";//a是一个字符数组。

char *p="string";// pis a string literal having static allocation. Any attempt to modify contents of pleads to Undefined Behavior since string literals are stored in read-only section of memory.

char *p="string";//p是具有静态分配的字符串文字。任何修改内容的尝试都会p导致未定义行为,因为字符串文字存储在内存的只读部分。

回答by jasonmp85

No difference. Unless you want to actually write to the array, in which case the whole world will explode if you try to use the second form. See here.

没有不同。除非你想真正写入数组,在这种情况下,如果你尝试使用第二种形式,整个世界都会爆炸。见这里

回答by Fyodor Soikin

First declaration declares an array, while second - a pointer.

第一个声明声明一个数组,而第二个声明是一个指针。

If you're interested in difference in some particular aspect, please clarify your question.

如果您对某些特定方面的差异感兴趣,请澄清您的问题。

回答by travis0xFF

One difference is that sizeof(a)-1 will be replaced with the length of the string at compile time. With p you need to use strlen(p) to get the length at runtime. Also some compilers don't like char *p="string", they want const char *p="string" in which case the memory for "string" is read-only but the memory for a is not. Even if the compiler does not require the const declaration it's bad practice to modify the string pointed to by p (ie *p='a'). The pointer p can be changed to point to something else. With the array a, a new value has to be copied into the array (if it fits).

一个区别是 sizeof(a)-1 将在编译时替换为字符串的长度。使用 p 您需要使用 strlen(p) 在运行时获取长度。还有一些编译器不喜欢 char *p="string",他们想要 const char *p="string" 在这种情况下,"string" 的内存是只读的,而 a 的内存不是。即使编译器不需要 const 声明,修改 p 指向的字符串也是一种不好的做法(即 *p='a')。指针 p 可以更改为指向其他内容。对于数组 a,必须将新值复制到数组中(如果合适)。