通过嵌套结构传递函数使用指针
问题描述:
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 (bracket
,在注释行中。通过嵌套结构传递函数使用指针
我怎样才能使用指针传递结构并在嵌套结构中输入数据?
答
二是不是一个参考,所以试图解引用它会导致错误。相反,你应该只解除四个。
void writing_a_value(three *four)
{
four->two.i=1; /*no problem here */
//(*four).two.i=1 would accomplish the same thing
}
我编辑你的代码因为它不可读。你收到什么错误消息? – DOOM 2014-10-07 16:22:15
0)'wriring_a_value' - >'writing_a_value' 1)'four - >(two-> i)= 1;' - >'four-> two.i = 1;'2)'printf(“%d “,two.i);' - >'printf(”%d“,four.two.i);' – BLUEPIXY 2014-10-07 16:24:37
你正在用一,二,三...命名来破坏自己。 – 2501 2014-10-07 16:53:20