gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 00:55:34 25 4
gpt4 key购买 nike

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

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com