这看起来应该很简单,但我花了太多时间。希望有人能帮助。更改字符指针数组中的内容
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
}
你不能 - 他们是常量。 – 2010-11-11 14:59:20
所以这个声明和const char * string_labels = {“one”,“two”,“three”,“four”,“five”}一样。 ?我想如果它没有被声明为const,它将会改变吗?你能解释为什么吗?感谢您的帮助 – 2010-11-11 15:02:42
是的,字符串文字是有效的const,即使它们没有被声明为这样。通常它们驻留在只读段中。 – 2010-11-11 15:06:57