what is null pointer in c and c++ and why it is used?
what is null pointer in c and c++ and why it is used?
A null pointer is a regular pointer of any pointer type which has a special value that indicates that it is not pointing to any valid reference or memory address. This value is the result of type-casting the integer value zero to any pointer type.
int * p;
p = 0; // p has a null pointer value
I Think..u can search in google...for better understanding..as to y this error occurs..wat exactly happens etc...
The null pointer constant does not to point to any real object. We can assign it to any pointer variable since it has type void *.
The NULL pointer points to an address of 0x00000000 (32 bits) of memory. The computer systems have reserved this zero address for NULL pointer and cannot be used for other purposes.
The null pointer is a well-defined "nowhere" that is guaranteed to compare unequal to any valid memory address. It can be used in several different ways:
- Indicate an error condition - standard library functions such as fopen and malloc will return a null pointer if they cannot complete an operation such as opening a file or allocating memory;
- Indicate the end of input or of a sequence - functions like fgets or strtok will return NULL if there's no more input (end of file or end of string);
- Indicate a pointer is not currently being used - since NULL is well-defined, it's easy to check against, so any pointer variable that's not currently meant to point anywhere should be set to NULL (in general, it's impossible to know if a non-NULL pointer value is valid or not just from the pointer value itself);
Note that there is a null pointer value (the address used by the underlying operating system to indicate "nowhere") and the null pointer constant (used to define the NULL macro). The operating system may use any address it wants to for NULL; it could be 0x00000000, it could be 0xFFFFFFFF, it could be 0xDEADBEEF, etc. However, as far as your source code is concerned, a zero-valued integral expression indicates a NULL pointer. The macro NULL is defined as either a plain 0, or as (void *) 0, or any other integral expression that evaluates to 0.
When the compiler translates your source code to machine code, it will replace all those instances of 0 or NULL with the correct null pointer value.