+ 7
Can someone please explain why (in what situation) a pointer is initialized with NULL?
I'm a little confused, since a pointer is a variable with the address of another variable as its value, so why the NULL? Thanks in advance.
8 Answers
+ 7
AK-47 Thanks so much! I get it now. The really easy answer I was looking for is that the pointer might otherwise get a garbage value, possibly resulting in mistakes in your code. So simple, but took me way too long to understand!
+ 5
here NULL is used to specify that pointer doesn't point to any location.
then why don't we use 0 ? why NULL?
because 0 can be an address to the memory span, NULL is used to denote the absence of any value.
+ 4
Nikhil Dhama I understand that NULL means the absence of value. My question was why it's relevant for a pointer, since the whole point of a pointer is that it has an address as its value. I'm still working through this material, so I might be completely wrong, but I assumed that usually means you know what variable and thereby what memory address you want attached to it?
+ 4
Jan Ć tÄch That makes sense to me, but why initialize a pointer explicitly using NULL or nullptr? According to your logic, a variable or pointer is given a null "value" by default (I know it's technically the absence of value, but for lack of better word - not a native English speaker here), which would mean there's no difference between declaring without value or initializing with NULL?
+ 1
N@G3
" [...] a variable or pointer is given a null "value" by default [...] "
Pointers have no specific default value. The default value is some random address of the memory (garbage).
" [...] which would mean there's no difference between declaring without value or initializing with NULL?"
Pointers always need to be initialized to some meaningful value (address). As in "case in point" section of my answer, the head and tail nodes need to be initialized to null value in order to represent an empty list in the begining.
+ 1
N@G3
Happy to hear that. good luck.
0
You can have empty pointers just as you can have empty variables:
int a;
//a is null
int *p;
//*p is null
int *p = &a;
//*p now contains adress to a
0
According to cppreference.com, The macro NULL is an implementation-defined null pointer constant, which may be
(Before C++11) an integral constant expression rvalue (e.g. T *pointer = NULL) of integer type that evaluates to 0. Implemented as
#define NULL 0
(After C++11) an integer literal with value 0, or a prvalue* of type std::nullptr_t (exclusive type of nullptr). Implemented as
#define NULL nullptr
~example~
int *ptr1 = NULL;
int *ptr2 = nullptr;
~case in point~
Initialization of a head and tail nodes in a singly linked list.
struct Node {
int data;
Node *next;
};
class List {
public:
List() {
head = nullptr;
tail = nullptr;
}
//...
private:
Node *head, *tail;
};
_____
* In C++11 terminology, refers to an rvalue that is not an xvalue (unnamed rvalue reference)
~example~
int f(); // returns prvalue
int &f(); // returns lvalue
int &&f(); // returns xvalue