這看起來應該很簡單,但我花了太多時間。希望有人能幫助。更改字符指針數組中的內容
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