Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
321 views
in Technique[技术] by (71.8m points)

c - Dereference a pointer inside a structure pointer

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.

question from:https://stackoverflow.com/questions/2581769/dereference-a-pointer-inside-a-structure-pointer

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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;


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...