The difference between the two statements? - C ++

I am a student programmer trying to better understand pointers, I found out that you can set the pointer to NULL. My question is, what is the difference between these two statements? When will each of them return true / false?

if (some_ptr == NULL) if (*some_ptr == NULL) 

Thanks!

+4
source share
5 answers

The first compares the comparison with the address of the variable with zero, the second plays the pointer, gets the value held on it, and compares it with zero.

+9
source

The first statement refers to the actual address pointed to by some_ptr. If NULL (the value represented by the NULL definition), it is true, otherwise not.

The last statement refers to the contents in the address pointed to by the pointer. Therefore, if you have a point some_ptr for an integer, and that integer matches your null definition, the second condition evaluates to true.

+3
source

First, you are comparing the pointer itself to NULL, which seems desirable.

Secondly, you first play the pointer to get a value that is then compared to NULL, for example, you compare the int value to 0. based on your variable name.

+1
source

The first one says:
Is some_ptr NULL?

The second says:
Is some_ptr pointing to NULL?

+1
source

For example: int * x; Here, if you like to check if x points to NULL, we use the first operator. With the same int * x, if you use the second operator, you try to dereference the pointer and check the value that x points to. Since NULL is 0 in C, C ++ checks for the value 0 that x points to.

EDIT: also with the second statement, if x points to NULL, then deferring the NULL pointer causes the kernel to crash on UNIX.

0
source

Source: https://habr.com/ru/post/1300577/


All Articles