0

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
  • It is the same thing, as would be int * ptr = &var. In this case * is not an operator, is just part of the type. Commented Jun 25, 2015 at 20:47
  • 4
    It will be easier to understand of you use int* ptr instead of int *ptr. Commented Jun 25, 2015 at 20:49
  • @DavidSchwartz unfortunately that's against language - int* ptr, a; becomes confusing, is a a pointer? while int *ptr, a; it is pretty clear that it is not. Commented Jun 25, 2015 at 21:23
  • int *ptr = NULL; is just short-hand for int *ptr; ptr = NULL; Commented Jun 25, 2015 at 21:53
  • @Slava Either way something is confusing. It's a choice of which is worse. Commented Jun 25, 2015 at 22:20

6 Answers 6

7

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".

Sign up to request clarification or add additional context in comments.

Comments

2

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; 

Comments

1

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".

Comments

0

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

Comments

0

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.

1 Comment

int* ptr{}; is even better.
0

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!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.