Can someone please explain why (in what situation) a pointer is initialized with NULL? | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
+ 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.

26th Jun 2018, 5:00 PM
N@G3
N@G3 - avatar
8 Réponses
+ 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!
26th Jun 2018, 9:30 PM
N@G3
N@G3 - avatar
+ 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.
26th Jun 2018, 5:46 PM
Nikhil Dhama
Nikhil Dhama - avatar
+ 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?
26th Jun 2018, 6:51 PM
N@G3
N@G3 - avatar
+ 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?
26th Jun 2018, 6:57 PM
N@G3
N@G3 - avatar
+ 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.
26th Jun 2018, 9:20 PM
To Seek Glory in Battle is Glorious
To Seek Glory in Battle is Glorious - avatar
+ 1
N@G3 Happy to hear that. good luck.
26th Jun 2018, 9:46 PM
To Seek Glory in Battle is Glorious
To Seek Glory in Battle is Glorious - avatar
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
26th Jun 2018, 5:27 PM
Jan Štěch
Jan Štěch - avatar
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
26th Jun 2018, 9:07 PM
To Seek Glory in Battle is Glorious
To Seek Glory in Battle is Glorious - avatar