在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 s[] = "Hello world";
这里,s是一个字符数组,如果我们愿意,它可以被覆盖。
char *s = "hello";
字符串字面值用于在内存中的某个地方创建这些字符块。我们可以通过改变它所指向的对象来重新赋值,但只要它指向一个字符串字面值,它所指向的字符块就不能被改变。
此声明:
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指针变量在内存中的位置取决于声明出现的位置(就像在第一个例子中一样)。
char s[] = "hello";
声明s为一个char数组,其长度足以容纳初始化式(5 + 1个字符),并通过将给定字符串文字的成员复制到数组中来初始化数组。
char *s = "hello";
声明为指向一个或多个(在本例中是多个)字符的指针,并将其直接指向一个包含文字“hello”的固定(只读)位置。
根据这里的注释,应该很明显:char * s = "hello"; 是一个坏主意,应该在非常狭窄的范围内使用。
这可能是指出“const正确性”是一件“好事”的好机会。无论何时何地,你可以使用“const”关键字来保护你的代码,不受“宽松”的调用者或程序员的影响,当指针开始发挥作用时,它们通常是最“宽松”的。
这就是用“const”修饰指针所能达到的效果。 (注意:必须从右向左阅读指针声明。) 以下是在使用指针时保护自己的3种不同方法:
const DBJ* p means "p points to a DBJ that is const"
-也就是说,DBJ对象不能通过p改变。
DBJ* const p means "p is a const pointer to a DBJ"
-也就是说,你可以通过p改变DBJ对象,但你不能改变指针p本身。
const DBJ* const p means "p is a const pointer to a const DBJ"
-也就是说,你不能改变指针p本身,也不能通过p改变DBJ对象。
与尝试常量突变相关的错误在编译时被捕获。const没有运行时空间或速度损失。
(当然,假设你使用的是c++编译器?)
——日本
补充一点:它们的大小也会有不同的值。
printf("sizeof s[] = %zu\n", sizeof(s)); //6
printf("sizeof *s = %zu\n", sizeof(s)); //4 or 8
如上所述,对于数组'\0'将被分配为最后一个元素。