【发布时间】:2017-01-04 22:35:48
【问题描述】:
我想从双指针访问结构的成员,但我得到了错误
“错误:‘(’标记之前的预期标识符”
:
struct test{
struct foo **val;
};
struct foo{
int a;
}
int main (){
struct test *ptr = (struct test *)malloc(sizeof(struct test));
ptr->val = &foo;
/*foo is already malloced and populated*/
printf ("Value of a is %d", ptr->(*val)->a);
}
我也试过了:
*ptr.(**foo).a
【问题讨论】:
-
你在寻找类似
(*(ptr->val))->a的东西吗? -
struct foo是一个类型。它没有地址。 -
@AlexD Aweseome,错误消失了,但值显示为“null”。我有一个 struct foo,它是 malloced 的,它的元素“a”在外部函数中被分配了一些值。为了保持这个值,我分配了一个指针 ptr->val = &foo。但是当我尝试打印这个值时,它显示为 null。
-
@user2816078 看到我的新答案!这就是你想要的吗?
-
除其他外,您在
struct foo的声明中缺少分号。
标签: c pointers struct operators pointer-to-pointer