You can simply hold an indirection:
C++:
struct S
{
void*& ptr; // ptr is now a reference to a pointer. Drawback: Has to be initialized.
};
This can be used as follows:
S s = {p};
But references as members are a controversial subject. In this case, the other way of indirection may be better:
C:
Maybe the new in your snippet was just for explanatory purposes, and you work with C (the elaborate type specifier in your code supports that argument).
struct S
{
void** ptr; // ptr is now a pointer to a pointer. Drawback: Can be zero.
};
In this case, you initialize ptr
with the address of the pointer it should refer to:
struct S s = {&p}; // I don't know about list-initialization in C, so i'll stick to this
Subsequently, when dereferencing it you'll get an lvalue to p
.
For differences between references and pointers, read this post.
And also don't forget about smart pointers. Maybe shared ownership is what you're looking for.