gpt4 book ai didi

c - 在 c 中第二次传递后,通过引用传递值不起作用

转载 作者:行者123 更新时间:2023-11-30 21:14:44 25 4
gpt4 key购买 nike

   int main()
{
int *x = 100;

display(&x);

}

display(int *m)
{
show(&m);
}

show(int *m)
{
printf("%d",*m)`;
}

输出应该是100。但它没有显示

最佳答案

你搞乱了类型。以下评论:

   int main()
{
int *x = 100; // x is a "pointer to int" (with bad assignment)

display(&x); // Since you use &x you are passing a "pointer to a pointer to int"

}

display(int *m) // but the function just expects a "pointer to int"
{
show(&m); // Here you do the same
// m is a "pointer to int"
// so &m is a "pointer to pointer to int"
}

show(int *m) // but the function just expects a "pointer to int"
{
printf("%d",*m)`;
}

要修复它,请执行以下操作:

   int main()
{
int x = 100; // x is a "int" (i.e. no *)

display(&x); // Passing a "pointer to int"

}

display(int *m)
{
show(m); // Just pass a "pointer to int" (i.e. no &) as
// m is already "pointer to int"
}

show(int *m)
{
printf("%d",*m)`;
}

关于c - 在 c 中第二次传递后,通过引用传递值不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39524676/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com