C Programming - Pointers - Discussion
Discussion Forum : Pointers - General Questions (Q.No. 1)
1.
What is (void*)0?
Discussion:
124 comments Page 4 of 13.
Murali said:
1 decade ago
void pointer is a generic pointer. In the sense it doesn't fall under any standard types.
A generic pointer or void pointer at address 0 is nothing but a null pointer.
A generic pointer or void pointer at address 0 is nothing but a null pointer.
Parveen said:
1 decade ago
i think here (void*) is just like type casting of 0 and * is used to represent 0. Hence it is null pointer
Balashankar said:
1 decade ago
Case 1:
printf("value =%u",(void*)0);
output is value=0;
Case 2:
printf("value =%u",(void*)1);
output is value=1;
Here in case 1 : void pointer acts as NULL pointer.
Null pointer has zero ('\0') as address.
printf("value =%u",(void*)0);
output is value=0;
Case 2:
printf("value =%u",(void*)1);
output is value=1;
Here in case 1 : void pointer acts as NULL pointer.
Null pointer has zero ('\0') as address.
Neha said:
1 decade ago
It seems type casting, as its for pointer address following it is 0, so it is null pointer.
Venu said:
1 decade ago
Void pointer is a pointer which can point to any data type. It's only purpose is to achieve compatibility between pointers which point to different data types.
Null pointer points to nowhere in the memory.
This problem might confuse because of the use of (void*) which is type casting to a void pointer but since the value after (void*) is zero so it leads to a null pointer.
Try this example:
#include<stdio.h>
void main()
{
int *p;
p=(void*)0;//replace 0 by any value, else part will be executed
if(p=='\0')
printf("null pointer\n");
else
printf("not a null pointer");
}
Null pointer points to nowhere in the memory.
This problem might confuse because of the use of (void*) which is type casting to a void pointer but since the value after (void*) is zero so it leads to a null pointer.
Try this example:
#include<stdio.h>
void main()
{
int *p;
p=(void*)0;//replace 0 by any value, else part will be executed
if(p=='\0')
printf("null pointer\n");
else
printf("not a null pointer");
}
Tejashwini said:
1 decade ago
Null pointers are the pointers which is pointing to nothing.
For example it can be represented as.
1) int *ptr=(char*)0;
2) char *ptr='\0';
or
3) int *ptr=null..
It can be for any data type.
When it comes to void pointers. The pointer will not point to any data and therefore cannot be dereferenced. It is still a pointer though, to use it you just have to cast it to another kind of pointer first.
For example it can be represented as.
1) int *ptr=(char*)0;
2) char *ptr='\0';
or
3) int *ptr=null..
It can be for any data type.
When it comes to void pointers. The pointer will not point to any data and therefore cannot be dereferenced. It is still a pointer though, to use it you just have to cast it to another kind of pointer first.
Arihant Jain said:
1 decade ago
Null pointer is a special reserved value of a pointer. A pointer of any type has such a reserved value. Formally, each specific pointer type (int *, char * etc.) has its own dedicated null-pointer value. Conceptually, when a pointer has that null value it is not pointing anywhere.
Void pointer is a specific pointer type - void * - a pointer that points to some data location in storage, which doesn't have any specific type.
So, null pointer is a value, while void pointer is a type. These concepts are totally different and non-comparable.
Void pointer is a specific pointer type - void * - a pointer that points to some data location in storage, which doesn't have any specific type.
So, null pointer is a value, while void pointer is a type. These concepts are totally different and non-comparable.
Spandya said:
1 decade ago
void pointer can store address of any data type value.
Declaration of Void Pointer :
void * pointer_name
Void Pointer Example :
void *ptr; // ptr is declared as Void pointer.
char cnum;
int inum;
float fnum;
ptr = &cnum; // ptr has address of character data.
ptr = &inum; // ptr has address of integer data.
ptr = &fnum; // ptr has address of float data.
Explanation :
void *ptr;
Void pointer declaration is shown above.
We have declared 3 variables of integer, character and float type.
When we assign address of integer to the void pointer, pointer will become Integer Pointer.
When we assign address of Character Data type to void pointer it will become Character Pointer.
Similarly we can assign address of any data type to the void pointer.
It is capable of storing address of any data type.
Declaration of Void Pointer :
void * pointer_name
Void Pointer Example :
void *ptr; // ptr is declared as Void pointer.
char cnum;
int inum;
float fnum;
ptr = &cnum; // ptr has address of character data.
ptr = &inum; // ptr has address of integer data.
ptr = &fnum; // ptr has address of float data.
Explanation :
void *ptr;
Void pointer declaration is shown above.
We have declared 3 variables of integer, character and float type.
When we assign address of integer to the void pointer, pointer will become Integer Pointer.
When we assign address of Character Data type to void pointer it will become Character Pointer.
Similarly we can assign address of any data type to the void pointer.
It is capable of storing address of any data type.
Nisha sahu said:
1 decade ago
In function we are using void as return type, in pointer why we are not using void as return type instead of void*.
Santhu84 said:
1 decade ago
#ifndef NULL
# define NULL ((void *) 0)
#endif
Then you can use NULL in different functions.
# define NULL ((void *) 0)
#endif
Then you can use NULL in different functions.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers