top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is the difference between a NULL Pointer and a NULL Macro?

+3 votes
763 views
What is the difference between a NULL Pointer and a NULL Macro?
posted Apr 21, 2016 by Prajwal C.m.

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+6 votes

Null macro is defined in stdio.h and stddef.h.It is used to represent a null pointer in code. its value is zero.

Null pointer is a pointer which has 0 or NULL value stored and points to points to 0x00 i.e. the first memory location of the OS.
A null pointer should not be confused with an uninitialized pointer.

answer Apr 21, 2016 by Bhaskar Kalaria
thanks for the answers.
Hey Can you explain when to use NULL macro.
Consider the sample 'C' program to demonstrate utilities of NULL macro

#include <stdio.h>      /* printf, scanf, NULL */
#include <stdlib.h>     /* malloc, free */

int main ()
{
  int i,len;
  char * buffer;

  printf (" string length?");
  scanf ("%d", &len);

  buffer = (char*) malloc (len+1);
  if (buffer == NULL)
        exit (1);

  for (i=0; i<len; i++)
    buffer[i]='i'+i;
  buffer[len]='\0';    //or buffer[len]=NULL is also valid

  printf ("string: %s\n",buffer);
  free (buffer);
  buffer = NULL;
}
...