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

Can you please explain the scope of static variables?

590


What is zero based addressing?

698


Explain modulus operator. What are the restrictions of a modulus operator?

591


Do pointers need to be initialized?

550


Why is c still so popular?

602






Differentiate between Macro and ordinary definition.

716


In cryptography, you could often break the algorithm if you know what was the original (plain) text that was encoded into the current ciphertext. This is called the plain text attack. In this simple problem, we illustrate the plain text attack on a simple substitution cipher encryption, where you know each letter has been substituted with a different letter from the alphabet but you don’t know what that letter is. You are given the cipherText as the input string to the function getwordSets(). You know that a plain text "AMMUNITION" occurs somewhere in this cipher text. Now, you have to find out which sets of characters corresponds to the encrypted form of the "AMMUNITION". You can assume that the encryption follows simple substitution only. [Hint: You could use the pattern in the "AMMUNITION" like MM occurring twice together to identify this]

1947


What is the use of sizeof () in c?

543


Explain how do you print only part of a string?

640


What is the scope of an external variable in c?

558


Explain the difference between ++u and u++?

627


What is the most efficient way to count the number of bits which are set in an integer?

576


What are the advantages and disadvantages of a heap?

692


How the c program is executed?

617


What is the use of a static variable in c?

578