C Programming - Pointers - Discussion
Discussion Forum : Pointers - General Questions (Q.No. 1)
1.
What is (void*)0?
Discussion:
124 comments Page 1 of 13.
Vishal Dalawai said:
1 decade ago
@Anurag.
Output : 2 1
The output of above program depends on the architecture(Little Endian/Big Endian).Intel processor uses Little Endian architecture.
Little Endian: LSB bits are stored in Lower memory locations & MSB bits are stored in Higher memory locations.
The binary equivalent of no 258(32 bit compiler)
(MSB) (LSB)
00000000 00000000 00000001 00000010
Suppose,the value 'num' stored in memory location 0x1000.Then the value 'num' stored in memory as below.
Address : 0x1000 0x1001 0x1002 0x1003
value : 00000010 00000001 00000000 00000000
Now,the integer pointer contains base address(0x1000).
The code '(char*)p' typecast the integer pointer to character pointer & points to address 0x1000 & *((char*)p) gives the value 2,which is 00000010.
The code '((char*)p+1)' typecast the integer pointer to character pointer & incrementing by 1.Now Pointer,pointing to memory location
0x1001 & *((char*)p+1) gives the value 1,which is 00000001
Note:Whenever pointer is increment/decrement,it is always increment/decrement of its data type size(char = 1Byte,Int = 4Bytes)
Output : 2 1
The output of above program depends on the architecture(Little Endian/Big Endian).Intel processor uses Little Endian architecture.
Little Endian: LSB bits are stored in Lower memory locations & MSB bits are stored in Higher memory locations.
The binary equivalent of no 258(32 bit compiler)
(MSB) (LSB)
00000000 00000000 00000001 00000010
Suppose,the value 'num' stored in memory location 0x1000.Then the value 'num' stored in memory as below.
Address : 0x1000 0x1001 0x1002 0x1003
value : 00000010 00000001 00000000 00000000
Now,the integer pointer contains base address(0x1000).
The code '(char*)p' typecast the integer pointer to character pointer & points to address 0x1000 & *((char*)p) gives the value 2,which is 00000010.
The code '((char*)p+1)' typecast the integer pointer to character pointer & incrementing by 1.Now Pointer,pointing to memory location
0x1001 & *((char*)p+1) gives the value 1,which is 00000001
Note:Whenever pointer is increment/decrement,it is always increment/decrement of its data type size(char = 1Byte,Int = 4Bytes)
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.
Dhiraj said:
1 decade ago
The void Type:
The void type specifies that no value is available. It is used in three kinds of situations:
S.N. Types and Description:
1. Function returns as void
There are various functions in C who do not return value or you can say they return void. A function with no return value has the return type as void. For example void exit (int status);
2. Function arguments as void
There are various functions in C who do not accept any parameter. A function with no parameter can accept as a void. For example int rand(void);
3. Pointers to void
A pointer of type void * represents the address of an object, but not its type. For example a memory allocation function void *malloc( size_t size ); returns a pointer to void which can be casted to any data type.
The void type specifies that no value is available. It is used in three kinds of situations:
S.N. Types and Description:
1. Function returns as void
There are various functions in C who do not return value or you can say they return void. A function with no return value has the return type as void. For example void exit (int status);
2. Function arguments as void
There are various functions in C who do not accept any parameter. A function with no parameter can accept as a void. For example int rand(void);
3. Pointers to void
A pointer of type void * represents the address of an object, but not its type. For example a memory allocation function void *malloc( size_t size ); returns a pointer to void which can be casted to any data type.
Naman_Shah said:
8 years ago
A null pointer is one which is not pointing to anything, i.e. it is assigned a null value. If there is no address to assign to a pointer, it is considered a good practice to set it to null.
Syntax: <data type> *<variable name> = NULL;
Example: int *ptr = NULL;
char *ptr = '\0';
A void pointer is one which does not have any data type associated with it, i.e. it can be assigned a value of any type. Also known as the general purpose pointer.
Example: void *ptr;
int a; char c;
ptr = &a; //ptr changes to integer pointer as address of integer is assigned to it
ptr = &c; //ptr changes to character pointer as address of character is assigned to it.
Syntax: <data type> *<variable name> = NULL;
Example: int *ptr = NULL;
char *ptr = '\0';
A void pointer is one which does not have any data type associated with it, i.e. it can be assigned a value of any type. Also known as the general purpose pointer.
Example: void *ptr;
int a; char c;
ptr = &a; //ptr changes to integer pointer as address of integer is assigned to it
ptr = &c; //ptr changes to character pointer as address of character is assigned to it.
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");
}
Gowda said:
1 decade ago
( void * ) is a void pointer..means its a pointer with no specific type...Integer zero 0 has various description depending upon the context in which it is used.
Each pointer has its own NULL values.In fact NULL is #defined in stddef.h to zero value.So NULL pointer basically means pointer containing zero or referencing no where...
for example
int main()
{
int *p=NULL;
if(p==0)
printf("hello");
return 0;
}
prints hello
and we have '\0' null char which is basically a value with all bits set zero...These null pointer and null char are for the purpose programmer convinience
Each pointer has its own NULL values.In fact NULL is #defined in stddef.h to zero value.So NULL pointer basically means pointer containing zero or referencing no where...
for example
int main()
{
int *p=NULL;
if(p==0)
printf("hello");
return 0;
}
prints hello
and we have '\0' null char which is basically a value with all bits set zero...These null pointer and null char are for the purpose programmer convinience
Debopam Pal said:
1 decade ago
Actually, when a data type is written within first bracket followed by a value then that value is converted to that data type, is called type casting. Likewise, when we use a pointer of any type within a first bracket followed by a value, then that pointer automatically points to that value. So, here both the data type (i.e pointer of any data type and the data type of the value) must be same. But when we use void type pointer that time we can use this syntax for any value, i.e (void*)0 -> a void type pointer points to 0 or NULL, i,e Null Pointer.
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.
DNY said:
1 decade ago
Dear friend (void *) is very imp pointer that c allow us to keep any type data under it.
I simply mean that if we want store adds of int I=10;
Into any pointer we required int *.
But when we use void pointer mean it can hold adds of any type of variable.
Int I=10;
Void * vptr=&I;
Now ans of above question:
When we assign 0 value to any pointer simply it mean it pointing to 0000 location of memory.
It mean it is not pointing to any variable so it is NULL pointer.
I simply mean that if we want store adds of int I=10;
Into any pointer we required int *.
But when we use void pointer mean it can hold adds of any type of variable.
Int I=10;
Void * vptr=&I;
Now ans of above question:
When we assign 0 value to any pointer simply it mean it pointing to 0000 location of memory.
It mean it is not pointing to any variable so it is NULL pointer.
Vishalakashi said:
8 years ago
NULL is used to indicate the pointer that doesn't point to a valid location. Ideally, we should initialize pointers as NULL if we don't know their value at the time of declaration. Also, we should make a pointer NULL when memory pointed by it is deallocated in the middle of a program.
Void pointer:it is a generic pointer that can point to any data type. We can assign any data type to void pointer and a void pointer can be assigned to a pointer of any data type.
Void pointer:it is a generic pointer that can point to any data type. We can assign any data type to void pointer and a void pointer can be assigned to a pointer of any data type.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers