Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the syntax of a pointer to a pointer?

Tags:

c

pointers

Let's say I have two structures,

struct ptr1
{
    struct ptr2 *ptrtoptr;
};

struct ptr2
{
    int name;
};

Main function looks like this:

int main()
{
    struct ptr1 *var1;
    struct ptr2 *var2;

    (code for manipulating structure element name);

    return 0;
}

How do a manipulate data of variable name via pointer var1? Let's say both pointers are already pointed at a certain address.

Is this correct? var1->(var2->name)=(some value) or (var1->var2)->name=(some value)?

like image 897
Kousaka Kirino Avatar asked Mar 16 '23 02:03

Kousaka Kirino


1 Answers

How do a manipulate data of variable name via pointer var1 ?

Using :

var1->ptrtoptr->name =  some_value ; // or (var1->ptrtoptr)->name

Neither of var1->(var2->name)=(some value) or (var1->var2)->name=(some value) make sense since var2 is not a member of ptr1, which can't be accessed using var1


Note: Also, be cautions about the operator associativity , operator -> has left to right associativity, therefore var1->(ptroptr->value) will not be same as var1->ptrtoptr->name

like image 111
P0W Avatar answered Mar 27 '23 17:03

P0W