Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dereference a pointer inside a structure pointer

Tags:

c

pointers

struct

I have a structure:

struct mystruct {     int* pointer; };  structure mystruct* struct_inst; 

Now I want to change the value pointed to by struct_inst->pointer. How can I do that?

EDIT

I didn't write it, but pointer already points to an area of memory allocated with malloc.

like image 580
Federico klez Culloca Avatar asked Apr 05 '10 23:04

Federico klez Culloca


People also ask

How do you dereference a pointer to a pointer?

x is a pointer to a pointer to int. *x yields a int* (pointer to int ) **x = is like *(*x) = so you first obtain the pointer to int then by dereferencing you are able to set the value at the address.

What happens if you dereference an initialized pointer?

Response:In Hi-Tech compiler if any pointer variable is modified during code execution, the compiler will give a warning "Dereferencing uninitialized pointer" if the pointer variable is not initialised with some address.To overcome this warning , initialize the pointer variable with any address during the pointer ...

Can we dereference a pointer?

Dereferencing is used to access or manipulate data contained in memory location pointed to by a pointer. *(asterisk) is used with pointer variable when dereferencing the pointer variable, it refers to variable being pointed, so this is called dereferencing of pointers.


1 Answers

As with any pointer. To change the address it points to:

struct_inst->pointer = &var;

To change the value at the address to which it points:

*(struct_inst->pointer) = var;

like image 190
Arkku Avatar answered Oct 12 '22 07:10

Arkku