[英]Changing pointer address - function
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". 然后打印“ aa”而不是“ ab”。 I know its probably pretty stupid question, but i don't know what is going and how to solve that issue.
我知道这可能是非常愚蠢的问题,但是我不知道会发生什么以及如何解决该问题。
In your por function, t will not be changed. 在您的por函数中,t不会更改。 You need change it
你需要改变它
void por(char **t){
*t = *t + sizeof(char);
}
and call it with por(&w) 并用por(&w)调用它
Try this: 尝试这个:
static char *por(char *t)
{
return t + sizeof(char);
}
int main(void)
{
char *w = "ab";
printf("%c",*w);
w = por(w);
printf("%c",*w);
return 0;
}
您正在增加该功能本地的副本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.