可能重复:
为什么在写入字符串时会出现分段错误?
我有以下程序:
char *s = "abcdf";
char s1[50] = "abcdf";
s1[0] = 'Q'; // Line 1
s[0] = 'P'; // Line 2
Run Code Online (Sandbox Code Playgroud)
为什么Line 1工作正常并Line 2导致程序崩溃?
假设我正在与串行端口设备通信并且具有用于控制这种设备的大量命令(74).这是存储和使用它们的最佳方式吗?
当然,我可以通过以下方式组织它们:
static char *cmd_msgs[] =
{
"start",
"stop",
"reset",
"quit",
"",
"",
"",
"",
...
};
Run Code Online (Sandbox Code Playgroud)
或人类可读:
char cmd_start_str[] = "start";
...
char cmd_quit_str[] = "quit";
Run Code Online (Sandbox Code Playgroud)
有人能指出一个处理这样一个任务的工作案例吗?
int main() {
int i;
char a[]={"Hello"};
while(a!='\0') {
printf("%c",*a);
a++;
}
getch();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
字符串存储在连续的内存位置,并在将地址传递给printf()时应该打印字符.我已经开始学习C.我无法找到答案.请帮忙.
我有一个关于memcopy的非常简单的问题,我已经能够修复但不明白为什么.以下代码在runtuime崩溃并出现"访问冲突":
char *src = "HELLO WORLD!";
char * dest = "hello world!";
memcpy(dest, src, strlen(src)+1);
Run Code Online (Sandbox Code Playgroud)
dest似乎足够大,据我所知,memcpy应该盲目地复制字节,所以我不明白这个问题.
将dest更改为数组例如可以char dest[13];解决问题,因此要求dest必须是未初始化的内存并创建数组才能为您执行此操作,而指针声明则不是吗?
干杯
char* a = "string"; /*"string" is a string literal, thus modifying
value isn't allowed, but then why */
char b[] = "string1"; /*"string1" is
also a string literal but why modification of this is allowed? */
a[1] = 's'; //this is not allowed b[1] = 'p'; //this is allowed
Run Code Online (Sandbox Code Playgroud)
为什么 char 数组明确指向字符串文字时可以修改?
我试图弄清楚为什么 C 会这样做。这是一个非常简单的字符串连接代码,但根据进入 strcat 的内容,我得到了非常不同的输出。
有人可以对此有所了解吗?
// With all declared variables
char * hello = "HELLO";
char * world = "WORLD";
char * space = " ";
char * say_hello(){
char * out = "";
strcat(out,hello);
strcat(out,space);
strcat(out,world);
return out;
}
main(){
puts(say_hello());
}
// outputs "HELLO WORLD"
Run Code Online (Sandbox Code Playgroud)
// With all hello as declared variable
char * hello = "HELLO";
char * say_hello(){
char * out = "";
strcat(out,hello);
strcat(out," ");
strcat(out,"WORLD");
return out;
}
main(){
puts(say_hello());
}
// outputs …Run Code Online (Sandbox Code Playgroud) 我对这个简单的事情进行了尴尬的斗争,因为这是段错误:
#include <stdio.h>
int main()
{
char *test = "this is a string";
test[0] = 'q';
printf(test);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但这不是:
#include <stdio.h>
int main()
{
char test[] = "this is a string";
test[0] = 'q';
printf(test);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
查看程序集后,我注意到在第一种情况下,文字"this is a string"是在 中声明的.rodata,因此这解释了段错误。但在第二种情况下,字符串根本不在程序集中,因此我假设它是通过 .data 部分链接为可写的。为什么会有这种行为差异?这很明显吗?我很愚蠢吗?
我读了几个类似的问题,C:字符指针和数组之间的差异,是什么个char []与char*S之间的区别?,char array []和char*array有什么区别?但他们似乎都不清楚我的怀疑.
我知道
char *s = "Hello world";
Run Code Online (Sandbox Code Playgroud)
使字符串不可变,而
char s[] = "Hello world";
Run Code Online (Sandbox Code Playgroud)
可以修改.
我怀疑的是,如果我这样做char stringA[LEN]; ,char* stringB[LEN];他们有什么不同吗?或者stringB在之前的情况下再次变得不可变?
我正在尝试使用 strstr() 在字符串中查找子字符串。仅使用 char[] 有效,char* 无效,导致分段错误。
所以这个正在工作:
int main() {
char str[] = "apple apple peach";
char *p;
p = strstr(str, "peach");
if (p!= NULL) {
strncpy(p, "apple", 5);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但这一个不起作用:
int main() {
char *str = "apple apple peach";
char *p;
p = strstr(str, "peach");
if (p!= NULL) {
strncpy(p, "apple", 5);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这个既不是:
int main() {
char *str = "apple apple peach";
char *peach = "peach";
char *p;
p = strstr(str, peach); …Run Code Online (Sandbox Code Playgroud) 是char a[] = {'a', '\0'}和char *b = "a"平等?
有什么不同?