When I want to assign a memory to the pointer in the function I have to pass the pointer by reference (or pointer), for example:
void fun(int*& ptr) //or int** ptr
{
ptr = new int(1);
}
int* ptr = nullptr;
fun(ptr);
int x = *ptr;
I have noticed that when I have a struct which contains a pointer passing by value works:
struct T
{
int* ptr{ nullptr };
};
void fun(T* t)
{
t->ptr = new int(1);
}
T *t = new T{};
fun(t);
int x = *(t->ptr);
Could you explain why in the second case I don't have to pass a pointer to the struct by reference or pointer ?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…