Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between a void pointer and a NULL pointer? [duplicate]

Tags:

c++

c

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?

like image 966
Sanjay Avatar asked Nov 07 '10 02:11

Sanjay


People also ask

What is the difference between void and null?

A void is nothing but takes up space; null is nothing at all. In other words, you could measure a void but null offers nothing to measure. 4.1 Void is used to indicate that a function/method does not return any data type. Null indicates that a pointer variable is not pointing to any address.

What is the difference between null and void C++?

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.

What is a void pointer?

A void pointer is a pointer that has no associated data type with it. A void pointer can hold address of any type and can be typecasted to any type.

What is the difference between null and void in Java?

Void means that a method does not return a result. Null means that an object reference does not currently reference an object (in other words, it references nothing).


2 Answers

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)
like image 149
Pablo Santa Cruz Avatar answered Nov 16 '22 01:11

Pablo Santa Cruz


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.

like image 40
Alexander Rafferty Avatar answered Nov 15 '22 23:11

Alexander Rafferty