这个问题可以作为所有常见问题的参考:
当我将数据复制/扫描到未初始化指针所指向的地址时,为什么会出现神秘崩溃或"分段错误"?
例如:
char* ptr;
strcpy(ptr, "hello world"); // crash here!
Run Code Online (Sandbox Code Playgroud)
要么
char* ptr;
scanf("%s", ptr); // crash here!
Run Code Online (Sandbox Code Playgroud) 案例1:我写的时候
char*str={"what","is","this"};
Run Code Online (Sandbox Code Playgroud)
然后str[i]="newstring";是有效的,而str[i][j]='j';无效.
案例2:我写的时候
char str[][5]={"what","is","this"};
Run Code Online (Sandbox Code Playgroud)
然后str[i]="newstring";无效,而str[i][j]='J';有效.
为什么会这样?我是一个初学者,在阅读其他答案后已经非常困惑.
我在C听说过,如果我这样做的话
char *s = "hello world".
Run Code Online (Sandbox Code Playgroud)
"hello world"实际上存储在只读存储器中.
我不太清楚只读内存.解释是什么?这是否像编译器的标志,告诉编译器不写入该部分?
我知道为了比较C中的两个字符串,您需要使用该strcmp()函数.但我试图将两个字符串与==运算符进行比较,并且它有效.我不知道如何,因为它只是比较两个字符串的地址.如果字符串不同,它应该不起作用.但后来我打印了字符串的地址:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char* str1 = "First";
char* str2 = "Second";
char* str3 = "First";
printf("%p %p %p", str1, str2, str3);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出是:
00403024 0040302A 00403024
Process returned 0 (0x0) execution time : 0.109 s
Press any key to continue.
Run Code Online (Sandbox Code Playgroud)
怎么可能str1和str3具有相同的地址?它们可能包含相同的字符串,但它们不是同一个变量.
我一直在C中使用字符串常量作为以下之一
char *filename = "foo.txt";
const char *s = "bar"; /* preferably this or the next one */
const char * const s3 = "baz":
Run Code Online (Sandbox Code Playgroud)
但是,看完这篇文章之后,现在我想知道是否应该将我的字符串常量声明为
const char s4[] = "bux";
Run Code Online (Sandbox Code Playgroud)
?
请注意,建议作为重复项的链接问题有所不同,因为该问题专门询问常量字符串。我知道类型如何不同以及如何存储。在这个问题的阵列版本不 const -qualified。这是一个简单的问题,即我是否应该对常量字符串使用常量数组,而不是我一直使用的指针版本。当在SO和Google上进行两天的搜索未得出确切答案时,这里的答案已经回答了我的问题。多亏了这些答案,我才知道当标记数组时,编译器可以做一些特殊的事情const,而且确实(至少有一种情况)我现在将使用数组版本。
我有两个问题......(我正在学习C,这可能是个愚蠢的问题.道歉)
根据如何在C语言和大多数书中声明字符串,即使你通过说分配内存,他们总是说声明一个字符串
char p2[] = "String";
Run Code Online (Sandbox Code Playgroud)我的问题是,无论如何要声明一个字符串?
根据/sf/answers/119310341/,在这样的示例中,
char s[]="hello";
Run Code Online (Sandbox Code Playgroud)放在只读区域,然后复制到数组.在C中打印字符串的地址是否有效?
printf("%p\n", &"Hello There"); // I tried, it prints some address
Run Code Online (Sandbox Code Playgroud)
并通过这样做
printf("%p\n", &"Hello There");
printf("%p\n", &"Hello There");
Run Code Online (Sandbox Code Playgroud)
它正在打印相同的地址.感觉是什么,它应该打印不同的地址.编译器在这里进行一些优化吗?
char a[]="string";和之间有什么区别char *p="string";?
我想知道char s[] = "hello"和之间的区别是什么char *s = "hello".
据我所知,内存中有五个数据段,Text,BSS,Data,Stack和Heap.
根据我的理解,
在以下情况下char s[] = "hello":
"hello" 在文本中.s 如果是全局变量,则在Data中;如果是局部变量,则在Stack中.
我们还有一个存储"hello"位置的副本s,因此我们可以修改此字符串的值s.
在以下情况下char *s = "hello":
"hello" 在文本中.s 如果是全局变量,则在Data中;如果是局部变量,则在Stack中.s只是指向"hello"Text并且我们没有它的副本,因此通过此指针修改字符串的值应该导致"Segmentation Fault".我对吗?
如果我写这个
char *array = "One good thing about music";
Run Code Online (Sandbox Code Playgroud)
我实际创建一个数组?我的意思是这样的一样吗?
char array[] = {"One", "good", "thing", "about", "music"};
Run Code Online (Sandbox Code Playgroud) 我正在学习C,我正在尝试使用指针来反转字符串.(我知道你可以使用一个数组;这更多的是关于学习指针.)
尝试运行下面的代码时,我不断遇到分段错误.海湾合作委员会似乎不喜欢这*end = *begin;条线.这是为什么?
特别是因为我的代码几乎与另一个问题中讨论的非邪恶C函数相同
#include <stdio.h>
#include <string.h>
void my_strrev(char* begin){
char temp;
char* end;
end = begin + strlen(begin) - 1;
while(end>begin){
temp = *end;
*end = *begin;
*begin = temp;
end--;
begin++;
}
}
main(){
char *string = "foobar";
my_strrev(string);
printf("%s", string);
}
Run Code Online (Sandbox Code Playgroud)