Explain Briefly Null Pointer Assignment C

 

5. Null Pointers

5.1 What is this infamous null pointer, anyway?

5.2 How do I get a null pointer in my programs?

5.3 Is the abbreviated pointer comparison ``'' to test for non-null pointers valid? What if the internal representation for null pointers is nonzero?

5.4 What is and how is it defined?

5.5 How should be defined on a machine which uses a nonzero bit pattern as the internal representation of a null pointer?

5.6 If were defined as follows:

#define NULL ((char *)0) wouldn't that make function calls which pass an uncast work?

5.7 My vendor provides header files that as . Why?

5.8 Is valid for pointers to functions?

5.9 If and are equivalent as null pointer constants, which should I use?

5.10 But wouldn't it be better to use (rather than ), in case the value of changes, perhaps on a machine with nonzero internal null pointers?

5.11 I once used a compiler that wouldn't work unless was used.

5.12 I use the preprocessor macro

#define Nullptr(type) (type *)0 to help me build null pointers of the correct type.

5.13 This is strange. is guaranteed to be , but the null pointer is not?

5.14 Why is there so much confusion surrounding null pointers? Why do these questions come up so often?

5.15 I'm confused. I just can't understand all this null pointer stuff.

5.16 Given all the confusion surrounding null pointers, wouldn't it be easier simply to require them to be represented internally by zeroes?

5.17 Seriously, have any actual machines really used nonzero null pointers, or different representations for pointers to different types?

5.18 Is a run-time integral value of 0, cast to a pointer, guaranteed to be a null pointer?

5.19 How can I access an interrupt vector located at the machine's location 0? If I set a pointer to , the compiler might translate it to some nonzero internal null pointer value.

5.20 What does a run-time ``null pointer assignment'' error mean? How can I track it down?


top


about this FAQ list   about eskimo   search   feedback   copyright

Hosted by


Previous Page

Next Page


We have already studied the different type of pointer in C. In this tutorial we will be learning about NULL Pointer in C Programming

What is Null Pointer ?

  1. NULL Pointer is a pointer which is pointing to nothing.
  2. NULL pointer points the base address of segment.
  3. In case, if you don’t have address to be assigned to pointer then you can simply use NULL
  4. Pointer which is initialized with NULL value is considered as NULL pointer.
  5. NULL is macro constant defined in following header files –
    • stdio.h
    • alloc.h
    • mem.h
    • stddef.h
    • stdlib.h

Defining NULL Value

#define NULL 0

We have noticed that 0 value is used by macro pre-processor. It is re-commanded that you should not use NULL to assign 0 value to a variable.

#include<stdio.h>intmain(){int num =NULL;printf("Value of Number ",++num);return0;}

Above program will never print 1, so keep in mind that NULL should be used only when you are dealing with pointer.

Re-commanded Articles : Declaring pointers | Initializing pointers

Visual Representation

Below are some of the variable representations of NULL pointer.

float*ptr =(float*)0;char*ptr =(char*)0;double*ptr =(double*)0;char*ptr ='\0';int*ptr =NULL;

you can see that we have assigned the NULL to only pointers.

Example of NULL Pointer

#include <stdio.h>intmain(){int*ptr =NULL;printf("The value of ptr is %u",ptr);return0;}

Output :

The value of ptr is 0

You can find some of the frequently asked questions about NULL pointer here


Previous Page

Next Page


0 thoughts on “Explain Briefly Null Pointer Assignment C

Leave a Reply

Your email address will not be published. Required fields are marked *