Hil*_*l3D 3 c++ pointers if-statement
After a pointer is initialized, do you have to use the * dereference operator to call the pointer in a condition?
Example:
int main()
{
int var = 10;
int *ptr = &var;
if(ptr) // does this need to be if(*ptr) ???
{.......}
}
Run Code Online (Sandbox Code Playgroud)
And can I have a short explanation as to why?
Thank you.
if (ptr)
Run Code Online (Sandbox Code Playgroud)
check if the pointer is not Null but
if (*ptr)
Run Code Online (Sandbox Code Playgroud)
check if the value it points to is not zero (in this example is 10)
So for checking the value you shoud add *.