Hello there.
I'm not new to c++, but pointers may confuse me in a specific case.
If i do this, it works well:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
int main()
{
int *OBJ = new int[1];
test(OBJ);
cout<<OBJ[0];
return 0;
}
void test(int *ob)
{
ob[0] = 55;
}
|
//Output: 55
But! If i send in one with a NULL value and allocate inside the function, it still will be NULL in main.
And OFC it will crash when i cout a NULL value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
int main()
{
int *OBJ = NULL;
test(OBJ);
cout<<OBJ[0];
return 0;
}
void test(int *ob)
{
ob=new int[1];
ob[0] = 55;
}
|
I try to understand why it is like that...
Also if i send in an already allocated array inside the function, and delete it first, then allocated it again with new, then it will print 55.
Its really making me confused. Feels like pointers sometimes are like call-by-reference and sometimes not.
An other question to:
Is there any difference between void test(int ob**) && void test(int ob*&) ?
Which one do you prefer? Know DirectX SDK really loves the double pointers.
Thank you!