C字符串:简单的问题 [英] C Strings: Simple Question

查看:116
本文介绍了C字符串:简单的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个变量初始化如下:

I have three variables initialised below :

char c1[] = "Hello";
char c2[] = { 'H', 'e', 'l', 'l', 'o', '\0'};
char* c3 = "Hello";

据我所知,c1和c2是相同的,并且它们都是字符串因为它们是由\\ 0终止。然而,C3是由C1和C2不同。这是由于C3不以\\ 0终止?这是否意味着C3是不是一个字符串?如果C3是不是一个字符串,那么为什么的printf(%S,C3); 不给错误?谢谢!

编辑:

是否有一个原因,C1和C2可以修改,但C3不能?

Is there a reason why c1 and c2 can be modified but c3 can't?

推荐答案

在C, C3 之间最中肯的差异方面和其他人是不允许你试图与修改底层人物C3 。我经常发现它有助于想到它是这样的:

In terms of C, the most pertinent difference between c3 and the others is that you are not allowed to attempt to modify the underlying characters with c3. I often find it helpful to think of it like this:

char *xyz = "xyz";

将创建一个可修改的指针的堆栈上,并使它指向了不可修改的字符序列 {'X','Y','Z','\\ 0'} 。另一方面,

will create a modifiable pointer on the stack and make it point at the non-modifiable character sequence {'x','y','z','\0'}. On the other hand,

char xyz[] = "xyz";

将创建一个可修改的阵列的堆栈大到足以容纳字符序列上的 {'X','Y','Z','\\ 0'} ,然后将该字符序列复制到它。然后,数组的内容会修改。请记住标准只字未提堆栈,但是这通常是它是如何做。这仅仅是一个帮助记忆,毕竟。

will create a modifiable array on the stack big enough to hold the character sequence {'x','y','z','\0'} and then copy that character sequence into it. The array contents will then be modifiable. Keep in mind the standard says nothing about stacks but this is commonly how it's done. It is just a memory aid, after all.

在形式上, C3 是一个指向一个字符串,而 C1 C2 这是发生都用空字符结束字符的两个数组。当他们传递给函数如的printf ,其腐烂的指针,这意味着他们将被同等对待,以数组的第一个元素C3 该函数内(实际上它们衰变下了不少的情况下,看到第三引自C99以下例外情况)。

Formally, c3 is a pointer to a string literal while c1 and c2 are both arrays of characters which both happen to end with a null character. When they're passed to functions like printf, they decay to a pointer to the first element of the array which means they'll be treated identically to c3 within that function (actually they decay under quite a few circumstances, see third quote from c99 below for exceptions).

C99的相关章节是 6.4.5字符串字面这也解释了为什么你不能修改什么 C3 指向:

The relevant sections of C99 are 6.4.5 String literals which explains why you're not allowed to modify what c3 points to:

这是不确定的,这些阵列是否提供了不同的元素具有适当的值。如果程序试图修改这样的阵列,其行为是不确定的。

It is unspecified whether these arrays are distinct provided their elements have the appropriate values. If the program attempts to modify such an array, the behavior is undefined.

和为什么它的确实的有一个空终止:

and why it does have a null terminator:

在翻译阶段7,零值字节或code追加到从字符串字面的结果或文字每个多字节字符序列。

In translation phase 7, a byte or code of value zero is appended to each multibyte character sequence that results from a string literal or literals.

6.3.2.1左值,数组和功能代号 6.3转换规定:

除了当它是sizeof操作符或一元和放大器的操作;运营商,或者是用于初始化数组,即具有类型'类型的数组''转换为类型的前pression指向指针类型'的前pression一个字符串文字数组对象的初始元素不是一个左值。如果数组对象有注册存储类,这种行为是未定义。

Except when it is the operand of the sizeof operator or the unary & operator, or is a string literal used to initialize an array, an expression that has type ‘‘array of type’’ is converted to an expression with type ‘‘pointer to type’’ that points to the initial element of the array object and is not an lvalue. If the array object has register storage class, the behavior is undefined.

这篇关于C字符串:简单的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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