Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to work with pointer to pointer to structure in C?

I want to change member of structure under double pointer. Do you know how?

Example code

typedef struct {
    int member;
} Ttype;

void changeMember(Ttype **foo) {
   //I don`t know how to do it
   //maybe
   *foo->member = 1;
}
like image 817
user43975 Avatar asked Dec 06 '08 20:12

user43975


People also ask

How you can build structure in C using a pointer?

In the above example, n number of struct variables are created where n is entered by the user. To allocate the memory for n number of struct person , we used, ptr = (struct person*) malloc(n * sizeof(struct person)); Then, we used the ptr pointer to access elements of person .

Can we have a pointer pointing to struct?

As we know Pointer is a variable that stores the address of another variable of data types like int or float. Similarly, we can have a Pointer to Structures, In which the pointer variable point to the address of the user-defined data types i.e. Structures.

Can you have a pointer to a pointer in C?

A pointer to a pointer is a form of multiple indirection, or a chain of pointers. Normally, a pointer contains the address of a variable. When we define a pointer to a pointer, the first pointer contains the address of the second pointer, which points to the location that contains the actual value as shown below.

How do you initialize a pointer to a structure?

Initialization of the Structure Pointer We can also initialize a Structure Pointer directly during the declaration of a pointer. As we can see, a pointer ptr is pointing to the address structure_variable of the Structure.


1 Answers

Try

(*foo)->member = 1;

You need to explicitly use the * first. Otherwise it's an attempt to dereference member.

like image 94
JaredPar Avatar answered Oct 06 '22 14:10

JaredPar