相关疑难解决方法(0)

通过指针更改C中的字符串文字?

可能重复:
为什么在写入字符串时会出现分段错误?

我有以下程序:

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导致程序崩溃?

c string

1
推荐指数
1
解决办法
2400
查看次数

最优雅的方式来存储串口设备的字符串命令?

假设我正在与串行端口设备通信并且具有用于控制这种设备的大量命令(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)

有人能指出一个处理这样一个任务的工作案例吗?

c serial-port

1
推荐指数
1
解决办法
171
查看次数

逐个字符地打印字符串

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.我无法找到答案.请帮忙.

c

1
推荐指数
1
解决办法
191
查看次数

memcpy使用指针抛出访问冲突

我有一个关于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必须是未初始化的内存并创建数组才能为您执行此操作,而指针声明则不是吗?

干杯

c

1
推荐指数
1
解决办法
408
查看次数

我认为字符串文字是只读的?

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++

1
推荐指数
1
解决办法
281
查看次数

C中连接变量和常量之间的区别

我试图弄清楚为什么 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)

c

1
推荐指数
1
解决办法
46
查看次数

为什么字符串文字根据声明为“char *”或“char[]”存储在不同的位置?

我对这个简单的事情进行了尴尬的斗争,因为这是段错误:

#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 assembly c-strings

1
推荐指数
1
解决办法
81
查看次数

C中的char stringA [LEN]和char*stringB [LEN]之间有什么区别?

我读了几个类似的问题,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在之前的情况下再次变得不可变?

c arrays string pointers

0
推荐指数
1
解决办法
143
查看次数

ANSI C strstr() 不使用指针

我正在尝试使用 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)

c strstr strncpy string.h

0
推荐指数
1
解决办法
820
查看次数

char a [] = {'a','\ 0'}和char*b ="a"相等?

char a[] = {'a', '\0'}char *b = "a"平等?

有什么不同?

c

0
推荐指数
1
解决办法
63
查看次数

标签 统计

c ×9

string ×2

arrays ×1

assembly ×1

c++ ×1

c-strings ×1

pointers ×1

serial-port ×1

string.h ×1

strncpy ×1

strstr ×1