What is the difference between null pointer and void pointer

Answer Posted / abi

Yes there are differences between a null pointer and a void
pointer. As you know that a pointer holds the memory address
of any variable . So we can say for a null pointer that :

- A null pointer can be of any primitive type(e.g - int *
,char * etc)

-However a null pointer does not point to any memory
location , i.e. it does not contain a reference of any
variable/value. Hence we may state that it has a NULL as its
value making it a null pointer.

Where as in case of a void pointer we can say :

- void pointer is always of type void *

- Mainly such pointers are used for dynamic memory
allocations (using malloc(), calloc() and realloc()
functions) , where memory block reserved is pointer to by a
void pointer and as the pointer value is returned it has to
be explicitly type casted to a particular desired type.

for e.g

int * ptr;

ptr=(int *) malloc(sizeof(int));

[ Here malloc () returns a pointer of void * type(i.e a void
pointer) which is type casted to int * as per our requirement ]

Is This Answer Correct ?    15 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Explain how can you avoid including a header more than once?

812


What is the use of pragma in embedded c?

780


What is the modulus operator?

929


Explain what are the different data types in c?

941


How do you define CONSTANT in C?

870


If i have an array 0 to 99 i.e,(Size 100) I place the values 1 to 100 randomly like a[0]=29,a[1]=56 upto array[99].. the values are only between 1 to 100. getting the array values by using scanf.. If i entered one wrong element value line a[56]=108. how can i find it.. and also how to find the missing value in 1 to 100.. and i want to replace the missing values.. any one of them know please post your answer..

1770


How to Throw some light on the splay trees?

793


How important is structure in life?

782


What are qualifiers?

797


How do we declare variables in c?

787


What is this infamous null pointer, anyway?

782


State the difference between realloc and free.

826


How do you write a program which produces its own source code as output?

829


I have seen function declarations that look like this

791


Is c programming hard?

769