Null pointers declaration confusion in C++

181 views Asked by At

I'm starting to study C++. I ran into a small confusion regarding pointers, specifically a NULL pointer. From my understanding when you declare a pointer and you set it to point to a value you can do the following:

    int var = 10;
    int *ptr;
    ptr = &var;
    *ptr = 20;

This would change the value of var to 20. But when you set a NULL pointer you do the following:

int *ptr = NULL;

Doesn't this mean you're assigning the value NULL to whatever variable ptr is pointing to not the address, because of the * operator? I thought a NULL pointer has a value (its address) as 0 so it points nowhere, from what I read. Would it make more sense to do this:

int *ptr;
ptr = NULL // or 0?

An explanation in layman terms would be appreciated, I'm still learning all the terms as I code and research so I'd love an explanation of any programming terms you use.

6

There are 6 answers

0
Jashaszun On BEST ANSWER

By saying int *ptr = NULL; you are saying "I am declaring an int-pointer called ptr that I want to point to location 0 (NULL). That's all it's saying.

Now, if you try to read or write to ptr you will get undefined behavior, which is generally a terrible place to be (much worse than just getting an error, because your program could start getting problems elsewhere and you won't know why). But generally, NULL pointers are used when you want to signify that it should not be used and must be initialized.

As David Schwartz said,

It will be easier to understand if you use int* ptr instead of int *ptr.

This is because the type of ptr is really int*, a pointer to an integer. When declaring types, * means pointer. It is only when using your pointers in expressions such as *ptr = 20 that the * means "dereference".

0
scohe001 On

NO. Everywhere you see the * operator with ptr it will mean dereferencing EXCEPT that first time. When you do

int *ptr = 0;

the * is making ptr an integer pointer and pointing it the null address, the "zero-th" location in memory

1
Steephen On

In modern C++ better usage is

int *ptr = nullptr;

It means your ptr don't have a location to deference in memory.

nullptr 's type is nullptr_t , it is not an integer or any other type. It is more safe instead depending on macro NULL. Since NULL can be converted as integer if it is defined as 0 and can create issues while handling function parameters and other scenarios.

0
fredoverflow On

You can replace the weird T* syntax with regular template syntax, thanks to C++11 alias templates:

template<typename T> using pointer = T*;

pointer<int> ptr = nullptr;
0
AnT stands with Russia On

In

int *ptr = NULL;

the * is not a * operator. The * character is simply a part of pointer declarator syntax. It has nothing to do with * operator whatsoever.

Operators are used in expressions. The above *ptr is a declarator in a declaration. Declarator is not an expression. There are no operators in that declarator, even if some characters look like "operators".

0
Gurleen Sethi On

In simplest of words:

int *ptr = NULL; 

means there is a pointer named ptr which has been assigned equal to NULL, which means right now it is not pointing anywhere(not at any garbage value or anything else) it has a value of NULL!