Reputation: 41
typedef struct {
int a;
} stu, *pstdu;
void func(stu **pst);
int main() {
stu *pst;
pst = (stu*)malloc(sizeof(stu));
pst->a = 10;
func(&pst);
}
void func(stu **pstu) {
/* how to access the variable a */
}
1) Want to access the structure variable a by passing the pointer address, in above function func.
2) In following case how it will behave example:
typedef struct {
int a;
} stu, *pstdu;
void func(pstdu *pst);
int main() {
stu *pst;
pst = (stu*)malloc(sizeof(stu));
pst->a = 10;
func(&pst);
}
void func(pstdu *pstu) {
/* how to access the variable a */
}
Upvotes: 0
Views: 2705
Reputation: 225817
You need to dereference the first pointer, then use the pointer-to-member operator:
(*pstu)->a
The parenthesis are required because the pointer-to-member operator has higher precedence than the dereference operator.
This is the same for both cases because stu **
and pstdu *
represent the same type. As mentioned in the comments, it's considered bad practice to have a pointer in a typedef
because it can hide that fact that a pointer is in use and can become confusing.
Upvotes: 4