Void pointers in C
In this article we are learning about “void pointers” in C language. Before going further it will be good if you refresh about pointers by reading – Introduction to pointers in C.
A pointer variable is usually declared with the data type of the “content” that is to be stored inside the memory location (to which the pointer variable points to).
Ex:- char *ptr; int *ptr; float *ptr;
A pointer variable declared using a particular data type can not hold the location address of variables of other data types. It is invalid and will result in a compilation error.
Ex:- char *ptr;
int var1;
ptr=&var1; // This is invalid because ‘ptr’ is a character pointer variable.
Here comes the importance of a “void pointer”. A void pointer is nothing but a pointer variable declared using the reserved word in C ‘void’.
Ex:- void *ptr; // Now ptr is a general purpose pointer variable
When a pointer variable is declared using keyword void – it becomes a general purpose pointer variable. Address of any variable of any data type (char, int, float etc.)can be assigned to a void pointer variable.
Dereferencing a void pointer
We have seen about dereferencing a pointer variable in our article – Introduction to pointers in C. We use the indirection operator * to serve the purpose. But in the case of a void pointer we need to typecast the pointer variable to dereference it. This is because a void pointer has no data type associated with it. There is no way the compiler can know (or guess?) what type of data is pointed to by the void pointer. So to take the data pointed to by a void pointer we typecast it with the correct type of the data holded inside the void pointers location.
Example program:-
#include
void main()
{
int a=10;
float b=35.75;
void *ptr; // Declaring a void pointer
ptr=&a; // Assigning address of integer to void pointer.
printf("The value of integer variable is= %d",*( (int*) ptr) );// (int*)ptr - is used for type casting. Where as *((int*)ptr) dereferences the typecasted void pointer variable.
ptr=&b; // Assigning address of float to void pointer.
printf("The value of float variable is= %f",*( (float*) ptr) );
}
The output:-
The value of integer variable is= 10
The value of float variable is= 37.75
A void pointer can be really useful if the programmer is not sure about the data type of data inputted by the end user. In such a case the programmer can use a void pointer to point to the location of the unknown data type. The program can be set in such a way to ask the user to inform the type of data and type casting can be performed according to the information inputted by the user. A code snippet is given below.
void funct(void *a, int z)
{
if(z==1)
printf("%d",*(int*)a); // If user inputs 1, then he means the data is an integer and type casting is done accordingly.
else if(z==2)
printf("%c",*(char*)a); // Typecasting for character pointer.
else if(z==3)
printf("%f",*(float*)a); // Typecasting for float pointer
}
Another important point you should keep in mind about void pointers is that – pointer arithmetic can not be performed in a void pointer.
Example:-
void *ptr;
int a;
ptr=&a;
ptr++; // This statement is invalid and will result in an error because 'ptr' is a void pointer variable.
最新文章
- IOS开发之——reveal 的使用
- hdu 5747 Aaronson
- 激活office 2010
- magento添加系统sections配置时应注意的事项
- 有了bootstrap,为什么还要做amaze ui
- 解决安装oracle后系统变慢问题
- Web 服务器 (IIS) 角色
- emoji图像转码解码 存入数据库
- sqlserver资源
- Beta冲刺Day1
- Mathematica查看内部定义
- Hbasewindows系统下启动报错及解决办法
- js+正则+单双引号问题
- python inspect.stack() 的简单使用
- Shell编程基础篇-上
- React componentDidMount
- python show slave status
- 一口一口吃掉Volley(三)
- php $_FILES处理文件上传
- java log4j日志配置