gpt4 book ai didi

c - 使用引用调用打印数组元素

转载 作者:太空宇宙 更新时间:2023-11-04 01:20:02 24 4
gpt4 key购买 nike

show() 的目的只是显示数组的元素。我的代码有什么问题?我的理解是,我们将单个元素的地址作为参数传递给 display(),然后将“x”的地址(存储元素的地址)传递给 show()。请帮忙!

#include <stdio.h>

void disp(int *);
void show(int *);
int main()
{
int i;
int marks[] = {55,65,89,78,74,77,45};
for(i=0;i<=6;i++)
{
disp(&marks[i]);
printf("%d \n", (&marks[i]));
}


return 0;
}
void disp(int *x)
{
show(&x);

}

void show(int *n)
{ printf("%d\n", *(*n));


}

最佳答案

当您在 disp() 中调用 show(&x) 时,您传递的是一个指向 int 的指针(因为 x 是一个指向 int 的指针),即使 show() 只接受一个指向 int 的指针。

您应该将 show() 中的参数从 int *n 更改为 int **n 以解决此问题。

关于c - 使用引用调用打印数组元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46478130/

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