NULL Pointer
Hello
people, today we are going to read about the null pointers used in C/C++. Why
are we using them? What is it’s significance? What we actually gonna gain by
typing ‘NULL’ in our code? These are the questions which crosses the mind of
every programmer. But the amount of attention given to these questions is very
less, and the importance of getting the answers of these questions is far more
important.
Just like normal
variables, pointers are not initialized when they are declared. Unless a value
is assigned, a pointer will point to some garbage address by default.
Besides memory addresses, there is one additional value that a
pointer can hold: a null value. A null value is
a special value that means the pointer is not pointing at anything. A pointer holding
a NULL value is called a null pointer.
So, we
initialize a pointer variable with NULL pointer when we don’t want to assign
the memory to the variable pointer.
For example,
int *ptr = NULL;
this implies
that the variable pointer ‘ptr’ is not assigned value yet.
Initialising
our pointer with the NULL pointer if not giving them any other value is
considered to be among good practices.
A
null pointer should not be confused with an uninitialized pointer: a null
pointer is guaranteed to compare unequal to any pointer that points to a valid
object. However, depending on the language and implementation, an uninitialized
pointer may not have any such guarantee. It might compare equal to other, valid
pointers; or it might compare equal to null pointers. It might do both at
different times. Or the comparison might be undefined behaviour.
So this is
all about ‘NULL’ pointer, thank you for giving it a read.
If you still have any doubt on this topic then do come to us via email "sophomoretechs@gmail.com" or via instagram "@coding.winds".
We also have an article on space complexity, give it a read by clicking here.
No comments:
Post a Comment