我要回答這個問題,測試這種重用在C數組變量:如何使用指針
練習19-16:修復示例中的代碼,以便採樣變量的值while循環之前保存然後運行,然後恢復。 while循環完成後,向代碼中添加puts(sample)語句,以證明該變量的原始地址已被恢復。
**Example:**
#include <stdio.h>
int main()
{
char *sample = "From whence cometh my help?\n";
while(putchar(*sample++))
;
return(0);
}
我想檢查我的答案是正確的,可能是你能給出一個解釋,因爲我對指針和變量沒有清晰的認識。 這就是我的回答:
#include <stdio.h>
int main()
{
char *sample = "From whence cometh my help?\n"; //This is my string
char StringSample[31];
int index = 0;
while(sample[index] != '\0') //while the char is NOT equal to empty char (the last one)
{
index++; //increase the index by 1
StringSample[index] = sample[index]; //put the char "n" inside the stringSample //
putchar(*sample++); //write it to the screen
}
putchar('\n'); //A new line
//to put the entire string on screen again
for (index=0; index<60; index++)
{
putchar(StringSample[index]);
}
return(0);
}
這裏是我的輸出:
我不知道爲什麼字符串它拆分到From whence co
爲什麼文本的其餘部分,即正如你所看到的,沒有任何意義。
我使用的Xcode 5.02
由於是明確的,現在, –