更改字符指针数组中的内容

标签 c

这看起来应该很容易,但我在这上面花了太多时间。希望有人能提供帮助。

char *string_labels[5] = { "one", "two", "three", "four", "five" };

void myFunction(void)
{

    //can print them just like expected
    for(i=0; i < 5; i++)
    {
        printf("%s\n", string_labels[i]);
    }

    //how can i change the contents of one of the elements??
    sprintf(string_labels[0], "xxx"); <-crashes

}

最佳答案

它崩溃是因为它在只读内存中。尝试

char string_labels[][6] = { "one", "two", "three", "four", "five" };
sprintf(string_labels[0], "xxx");

关于更改字符指针数组中的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4155699/

相关文章:

c - Linux线程id回收策略

将输入字符串中的单词 "welcome"更改为大写

c - 打印一个数字的英文单词的程序

c - 没有编译错误、结构和二维数组问题,而且我从未到达第二个打印语句?

c - 在 ubuntu 中使用原始套接字进行 TCP SYN 泛洪

c - 在离开启动子线程之前是否必须等待子线程完成?

c - Visual Studio 2008错误:ATL

c++ - 在 C/C++ 中自动调用函数

c - fork : number of processes created

C — 十六进制字符串到文字十六进制值