我要回答这个问题,测试这种重用在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
由于是明确的,现在, –