我知道您可以通过指针间接传递值:您指定将某个地址(指针)处的值(*)分配为等于某物(例如字符)。
// This works: I can pass char indirectly via pointers
// by saying "value at char pointer line equals c1 or c2"
char c1 = 'a';
char c2 = 'b';
char* line;
*line = c1;
*(line+1) = c2;
我可以使用上面的 *(line+1) 向右滑动内存空间。但是,当我循环它时,这会失败(如下):
// Output: "Process returned -1073741819 (0xC0000005)"
// Why?
char c;
char* line1;
int i = 0;
while ((c = getchar()) != EOF){
*(line1+i) = c;
i++;
}
输出:“进程返回 -1073741819 (0xC0000005)”
当我尝试在 while 循环中迭代时,为什么无法通过指针间接传递值?非常感谢!