在C语言中,可以在这样的声明中使用字符串字面值:
char s[] = "hello";
或者像这样:
char *s = "hello";
那么有什么不同呢?我想知道在编译和运行时,在存储持续时间方面实际发生了什么。
在C语言中,可以在这样的声明中使用字符串字面值:
char s[] = "hello";
或者像这样:
char *s = "hello";
那么有什么不同呢?我想知道在编译和运行时,在存储持续时间方面实际发生了什么。
当前回答
char s[] = "hello";
声明s为一个char数组,其长度足以容纳初始化式(5 + 1个字符),并通过将给定字符串文字的成员复制到数组中来初始化数组。
char *s = "hello";
声明为指向一个或多个(在本例中是多个)字符的指针,并将其直接指向一个包含文字“hello”的固定(只读)位置。
其他回答
char *str = "Hello";
上面设置str指向“Hello”字面值,该字面值是硬编码在程序的二进制图像中,在内存中被标记为只读,这意味着这个String字面值的任何更改都是非法的,并且会抛出分割错误。
char str[] = "Hello";
将字符串复制到堆栈上新分配的内存中。因此,对它进行任何更改都是允许的和合法的。
means str[0] = 'M';
将str更改为“Mello”。
如欲了解更多详情,请浏览类似问题:
为什么我得到一个分割错误时写入一个字符串初始化“char *s”而不是“char s[]”?
此声明:
char s[] = "hello";
Creates one object - a char array of size 6, called s, initialised with the values 'h', 'e', 'l', 'l', 'o', '\0'. Where this array is allocated in memory, and how long it lives for, depends on where the declaration appears. If the declaration is within a function, it will live until the end of the block that it is declared in, and almost certainly be allocated on the stack; if it's outside a function, it will probably be stored within an "initialised data segment" that is loaded from the executable file into writeable memory when the program is run.
另一方面,这个声明:
char *s ="hello";
创建两个对象:
一个6个字符的只读数组,包含值'h', 'e', 'l', 'l', 'o', '\0',它没有名称,具有静态存储持续时间(意味着它存在于程序的整个生命周期中);而且 一个类型为指针到字符的变量,称为s,用该未命名只读数组中第一个字符的位置初始化。
未命名的只读数组通常位于程序的“文本”段中,这意味着它与代码本身一起从磁盘加载到只读内存中。s指针变量在内存中的位置取决于声明出现的位置(就像在第一个例子中一样)。
另外,考虑到对于只读目的,两者的使用是相同的,您可以通过使用[]或*(<var> + <index>)索引来访问一个char。 格式:
printf("%c", x[1]); //Prints r
And:
printf("%c", *(x + 1)); //Prints r
很明显,如果你试图这么做
*(x + 1) = 'a';
你可能会得到一个分割错误,因为你试图访问只读内存。
给出声明
char *s0 = "hello world";
char s1[] = "hello world";
假设下面的内存映射(列表示从给定行地址偏移0到3的字符,例如右下角的0x00在地址0x0001000C + 3 = 0x0001000F):
+0 +1 +2 +3 0x00008000: 'h' 'e' 'l' 'l' 0x00008004: 'o' ' ' 'w' 'o' 0x00008008: 'r' 'l' 'd' 0x00 ... s0: 0x00010000: 0x00 0x00 0x80 0x00 s1: 0x00010004: 'h' 'e' 'l' 'l' 0x00010008: 'o' ' ' 'w' 'o' 0x0001000C: 'r' 'l' 'd' 0x00
字符串字面值"hello world"是一个12元素的char数组(c++中的const char),具有静态存储持续时间,这意味着它的内存在程序启动时分配,并一直分配到程序结束。试图修改字符串文字的内容会调用未定义的行为。
这条线
char *s0 = "hello world";
将s0定义为指向具有自动存储持续时间的char的指针(意味着变量s0仅存在于声明它的作用域),并将字符串字面值的地址(本例中为0x00008000)复制到它。注意,由于s0指向一个字符串字面值,它不应该被用作任何试图修改它的函数的参数(例如,strtok(), strcat(), strcpy()等)。
这条线
char s1[] = "hello world";
将s1定义为具有自动存储持续时间的12元素char数组(长度取自字符串字面量),并将字面量的内容复制到数组中。正如你从内存映射中看到的,我们有两个字符串"hello world"的副本;不同之处在于,您可以修改s1中包含的字符串。
S0和s1在大多数情况下是可以互换的;以下是例外情况:
sizeof s0 == sizeof (char*)
sizeof s1 == 12
type of &s0 == char **
type of &s1 == char (*)[12] // pointer to a 12-element array of char
您可以重新分配变量s0以指向不同的字符串字面值或另一个变量。不能将变量s1重新赋值以指向不同的数组。
char s[] = "hello";
声明s为一个char数组,其长度足以容纳初始化式(5 + 1个字符),并通过将给定字符串文字的成员复制到数组中来初始化数组。
char *s = "hello";
声明为指向一个或多个(在本例中是多个)字符的指针,并将其直接指向一个包含文字“hello”的固定(只读)位置。