这看起来应该很容易,但我在这上面花了太多时间。希望有人能提供帮助。
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/