0
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 */
}
dbush
  • 205,898
  • 23
  • 218
  • 273
santhosh kgn
  • 41
  • 1
  • 7

1 Answers1

4

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.

dbush
  • 205,898
  • 23
  • 218
  • 273
  • in 2nd case how it is working(void func(pstdu *pstu)- what is pstdu *pstu, whether *pstu is pointer to pointer – santhosh kgn Aug 17 '16 at 18:36
  • @sathish dbush didn't address the second example is its premise is wrong. – 2501 Aug 17 '16 at 18:39
  • 3
    @sathish related, [stop hiding pointer types in typedefs](https://stackoverflow.com/questions/750178/is-it-a-good-idea-to-typedef-pointers) and that problem becomes irrelevant. It's considered by most to be bad practice in all but a *very* limited number of circumstances (of which this is *not* one). It usually ends up accomplishing nothing but obfuscating the code and thereby making it harder to work with (which you're finding out). – WhozCraig Aug 17 '16 at 18:41