gpt4 book ai didi

c - 使用指针将嵌套结构传递给函数

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

typedef struct
{
int i;
}one;

typedef struct
{
one two;
}three;

void writing_a_value(three *);

int main()
{
three four;
writing_a_value(&four);
printf("%d",four.two.i);
}

void writing_a_value(three *four)
{
four->(two->i)=1; /* problem here */
}

我试过用(four->(two->i))=1这样的大括号,但还是不行。我必须传递指针,因为我必须将数据输入到嵌套结构中。error=expected (括号,在注释行中。

如何使用指针传递结构并在嵌套结构中输入数据?

最佳答案

two 不是引用,因此尝试取消引用它会导致错误。相反,您应该只取消引用四个。

void writing_a_value(three *four)
{
four->two.i=1; /*no problem here */
//(*four).two.i=1 would accomplish the same thing
}

关于c - 使用指针将嵌套结构传递给函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26240624/

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