这个例子看起来很简单,我不知道为什么我不知道确切的解释..
在下面的例子中:
#include <cstdio>
void scan(char* p){
printf("%c", *p++);
}
int main() {
char str[] = "hi this is a test";
char* p = str;
while(*p != '\0'){
printf("%c", *p++); //ok..
// scan(p); //crash
}
}
使用扫描功能有什么问题。即,如果这甚至是问题所在,为什么 p 在外层没有增加?
最佳答案
ie why does p not get incremented in the outer level if that is even the problem?
您的函数在该指针的拷贝上运行,因此递增不会影响原始指针。
要实现通过引用传递该指针变量:
void scan(char*& p){
printf("%c", *p++);
}
这是一个 working example online .
关于c++ - 简单的指针问题(我不知道为什么我没有得到确切的解释),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55730153/