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.
Void pointers in C的更多相关文章
- Pointer arithmetic for void pointer in C
http://stackoverflow.com/questions/3523145/pointer-arithmetic-for-void-pointer-in-c When a pointer t ...
- (转) Pointers
原地址 http://www.cplusplus.com/doc/tutorial/pointers/ Pointers In earlier chapters, variables have bee ...
- clang_intprt_t类型探究
作者:玄魂工作室-钱海龙 问题 这篇手把手教你构建 C 语言编译器,里面有着这样的代码 void eval() { int op, *tmp; while (1) { if (op == IMM) { ...
- C语言指针转换为intptr_t类型
1.前言 今天在看代码时,发现将之一个指针赋值给一个intptr_t类型的变量.由于之前没有见过intptr_t这样数据类型,凭感觉认为intptr_t是int类型的指针.感觉很奇怪,为何要将一个指针 ...
- 有关stdint.h 文件
有关stdint.h 文件 Google C++编程规范的P25页有如下叙述: <stdint.h> 定义了 int16_t . uint32_t . int64_t 等整型,在需要确定大 ...
- c语言二叉树
Department of Computing and Information SystemsCOMP10002 Foundations of AlgorithmsSemester 2, 2014As ...
- C语言指针的长度和类型
本文地址:http://www.cnblogs.com/archimedes/p/point-length-type.html,转载请注明源地址. 如果考虑应用程序的兼容性和可移植性,指针的长度就是一 ...
- C++程序结构---1
C++ 基础教程Beta 版 原作:Juan Soulié 翻译:Jing Xu (aqua) 英文原版 本教程根据Juan Soulie的英文版C++教程翻译并改编. 本版为最新校对版,尚未定稿.如 ...
- stdint.h 文件 int8_t uint8_t int16_t uint16_t
http://blog.chinaunix.net/uid-26588712-id-3068151.html c++ 数据类型 按照posix标准,一般整型对应的*_t类型为:1字节 uint ...
随机推荐
- 如何设置zencart买满多少免运费?
有时候会希望客户买满多少免运费,当订单总金额大于免运费的订单金额设值时,免运费.下面介绍一下zencart设置买满多少免运费: 1.进入后台–模块管理(Modules)–总额计算(Order Tota ...
- okhttp同步请求流程和源码分析
在上一次[http://www.cnblogs.com/webor2006/p/8022808.html]中已经对okhttp的同步与异步请求的基本使用有了一了初步了解,这次来从源码的角度来分析一下同 ...
- [易学易懂系列|rustlang语言|零基础|快速入门|(24)|实战2:命令行工具minigrep(1)]
[易学易懂系列|rustlang语言|零基础|快速入门|(24)|实战2:命令行工具minigrep(1)] 项目实战 实战2:命令行工具minigrep 有了昨天的基础,我们今天来开始另一个稍微有点 ...
- vue项目搭建和开发流程 vue项目配置ElementUI、jQuery和Bootstrap环境
目录 一.VUE项目的搭建 1. 环境搭建 2. 项目的创建和启动 二. 开发项目 1. 配置vue项目启动功能 2. 开发vue项目 (1)项目文件的作用 (2)vue项目开发流程 (3)vue项目 ...
- BZOJ 2219 数论之神 (CRT推论+BSGS+原根指标)
看了Po神的题解一下子就懂了A了! 不过Po神的代码出锅了-solve中"d-temp"并没有什么用QwQQwQQwQ-应该把模数除以p^temp次方才行. 来自BZOJ讨论板的h ...
- django查询优化及ajax编码格式下发送数据 总结
orm查询优化 1)only与refer only方法返回的是一个queryset对象,本质就是列表套数据对象 该对象内只含有only括号所指定的属性(其他属性也可以获取,但是需要重新走数据库 ...
- 2017"百度之星"程序设计大赛 - 初赛(A) [ hdu 6108 小C的倍数问题 ] [ hdu 6109 数据分割 ] [ hdu 6110 路径交 ] [ hdu 6112 今夕何夕 ] [ hdu 6113 度度熊的01世界 ]
这套题体验极差. PROBLEM 1001 - 小C的倍数问题 题 OvO http://acm.hdu.edu.cn/showproblem.php?pid=6108 (2017"百度之星 ...
- php回顾(3)系统函数
abs() 绝对值 ceil() 向上取整 floor() 向下取整 round() 四舍五入 第二个参数:保留小数点后面几位 ...
- java常用数据类型转换
在Java开发过程中经常会涉及到数据类型的转换问题,比如数字型转字符型,字符型转日期型,字符串转数组等等,以及其他类型的强制转换等.经常出现,所以有必要总结一下. 1.如何将字串 String 转换成 ...
- MacBook pro管理员变成普通用户无法解锁问题
最近使用Mac的时候,把管理员的名字修改了一下,WTF?没有管理员权限了?然后就没有解锁权限了,这可以说是Mac的一个致命bug呀!下载软件也不可以了.具体我的解决方式在这里记录一下,以供和我遇到同样 ...