更改指针地址 - 函数
我的指针有问题。 这工作正常 -
int main(void){
char *w;
w = calloc(20, sizeof(char));
w = "ab";
printf("%c",*w);
w = w + sizeof(char);
printf("%c",*w);
return 0;
}
但如果我使用如下函数:
void por(char *t){
t = t + sizeof(char);
}
然后
int main(void){
char *w;
w = calloc(20, sizeof(char));
w = "ab";
printf("%c",*w);
por(w);
printf("%c",*w);
return 0;
}
它会打印“aa”而不是“ab”。 我知道这可能是一个非常愚蠢的问题,但我不知道发生了什么以及如何解决该问题。
I have problem with pointers.
This is working fine -
int main(void){
char *w;
w = calloc(20, sizeof(char));
w = "ab";
printf("%c",*w);
w = w + sizeof(char);
printf("%c",*w);
return 0;
}
but if i use function like:
void por(char *t){
t = t + sizeof(char);
}
and
int main(void){
char *w;
w = calloc(20, sizeof(char));
w = "ab";
printf("%c",*w);
por(w);
printf("%c",*w);
return 0;
}
then it prints "aa" instead of "ab".
I know its probably pretty stupid question, but i don't know what is going and how to solve that issue.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在您的 por 函数中, t 不会改变。您需要更改它
并使用 por(&w) 调用它
In your por function, t will not be changed. You need change it
and call it with por(&w)
试试这个:
Try this:
您正在增加函数本地的副本。
you are incrementing the copy which is local to the function.