7

Possible Duplicate:
What's the difference between a null pointer and a void pointer?

What is the difference between a pointer to void and a NULL pointer in C? Or are they the same?

Community
  • 1
  • 1
Sanjay
  • 79
  • 1
  • 3
  • 1
    Exact duplicate of [What's the difference between a null pointer and a void pointer?](http://stackoverflow.com/questions/3581585/whats-the-difference-between-a-null-pointer-and-a-void-pointer). Please use search before posting questions. – qrdl Nov 07 '10 at 05:22

3 Answers3

10

In C, there is void, void pointer and NULL pointer.

  1. void is absence of type. I.E. a function returning a void type is a function that returns nothing.
  2. void pointer: is a pointer to a memory location whose type can be anything: a structure, an int, a float, you name it.
  3. A NULL pointer is a pointer to location 0x00, that is, no location. Pointing to nothing.

Examples:

void function:

void printHello()
{
   printf("Hello");
}

void pointer:

void *malloc(size_t si)
{
    // malloc is a function that could return a pointer to anything
}

NULL pointer:

char *s = NULL;
// s pointer points to nowhere (nothing)
Pablo Santa Cruz
  • 176,835
  • 32
  • 241
  • 292
7

void is a datatype. void* is just a pointer to an undefined type. A void* can be set to any memory location. A NULL pointer is a any pointer which is set to NULL (0).

So yes, they are different, because a void pointer is a datatype, and a NULL pointer refers to any pointer which is set to NULL.

Alexander Rafferty
  • 6,134
  • 4
  • 33
  • 55
5

Pointer to void is a pointer to an unspecified type. Ie. Just a pointer. It can still be a valid pointer, but we don't know what it points to (eg. A function might take a void pointer as a parameter, and then interpret the type according to a different parameter)

NULL is an 'empty' pointer. Not valid, can be used to specify a pointer to nothing / not set. It is a value whilst void is a type.

winwaed
  • 7,645
  • 6
  • 36
  • 81