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的更多相关文章

  1. Pointer arithmetic for void pointer in C

    http://stackoverflow.com/questions/3523145/pointer-arithmetic-for-void-pointer-in-c When a pointer t ...

  2. (转) Pointers

    原地址 http://www.cplusplus.com/doc/tutorial/pointers/ Pointers In earlier chapters, variables have bee ...

  3. clang_intprt_t类型探究

    作者:玄魂工作室-钱海龙 问题 这篇手把手教你构建 C 语言编译器,里面有着这样的代码 void eval() { int op, *tmp; while (1) { if (op == IMM) { ...

  4. C语言指针转换为intptr_t类型

    1.前言 今天在看代码时,发现将之一个指针赋值给一个intptr_t类型的变量.由于之前没有见过intptr_t这样数据类型,凭感觉认为intptr_t是int类型的指针.感觉很奇怪,为何要将一个指针 ...

  5. 有关stdint.h 文件

    有关stdint.h 文件 Google C++编程规范的P25页有如下叙述: <stdint.h> 定义了 int16_t . uint32_t . int64_t 等整型,在需要确定大 ...

  6. c语言二叉树

    Department of Computing and Information SystemsCOMP10002 Foundations of AlgorithmsSemester 2, 2014As ...

  7. C语言指针的长度和类型

    本文地址:http://www.cnblogs.com/archimedes/p/point-length-type.html,转载请注明源地址. 如果考虑应用程序的兼容性和可移植性,指针的长度就是一 ...

  8. C++程序结构---1

    C++ 基础教程Beta 版 原作:Juan Soulié 翻译:Jing Xu (aqua) 英文原版 本教程根据Juan Soulie的英文版C++教程翻译并改编. 本版为最新校对版,尚未定稿.如 ...

  9. stdint.h 文件 int8_t uint8_t int16_t uint16_t

    http://blog.chinaunix.net/uid-26588712-id-3068151.html c++ 数据类型 按照posix标准,一般整型对应的*_t类型为:1字节     uint ...

随机推荐

  1. python的索引与切片和元祖

    '''索引: 1.索引从0开始 2.末尾元素为 -1 3.能被for循环,有序的数据集合 切片: 1.顾头不顾尾 2.a = "123abcdfg" print(a[0::2]) ...

  2. Linux日常之命令awk

    参考:http://www.zsythink.net/archives/tag/awk/ 一. 命令awk简介 1. awk是一种编程语言,用于对文本和数据进行处理的 2. 具有强大的文本格式化能力 ...

  3. WCF契约定义及主要用途

    我们在使用WCF时,对其制定各种各样的规则,就叫做WCF契约.任何一个分布式的应用程序在传递消息的时候都需要实现制定一个规则. WCF配置文件相关操作技巧解析 全方位解读WCF Address配置文件 ...

  4. Ubuntu18.04系统执行语句时出现错误Failed to load module "canberra-gtk-module"

    Ubuntu18.04系统执行gnuradio-companion时,命令行提示错误Failed to load module "canberra-gtk-module",虽然看起 ...

  5. elementUI + vue 输入框只能输入正整数 不能输入字母 e 以及+ - 号

    <el-input :inline="true" v-model="dialogForm.closeTime" onKeypress="retu ...

  6. MySQL 关于索引的操作

    -- 索引分类? 1.普通索引 2.唯一索引 3.全文索引 4.组合索引 普通索引:仅加速查询,最基本的索引,没有任何限制 唯一索引:加速查询 + 列值唯一(可以有null) 全文索引:仅适用于MyI ...

  7. Java方法调用机制

    最近在编程时,修改方法传入对象的对象引用,并没有将修改反映到调用方法中.奇怪为什么结果没有变化,原因是遗忘了Java对象引用和内存分配机制.本文介绍3个点: ① 该问题举例说明 ② 简要阐述Java内 ...

  8. Chrome安卓H5调试,连接手机检测不到页面

    Chrome安卓H5调试,连接手机检测不到页面,重启什么的都不行,未找到设备,或者offline,怎么办? 首先手机开启调试模式是必须的 然后用adb工具箱,cmd进来 运行命令 adb kill-s ...

  9. javascript类型判断最佳实践

    javascript有8种数据类型 值类型 Number Null Undefined String Symbol Boolean BigInt 引用类型 Object Array Function ...

  10. Confluence 6 删除一个附加的文件

    你需要具有 删除附件(Delete Attachment)的空间权限来删除一个附加的文件. 希望删除一个附加文件的所有版本: 进入含有附件的页面中. Go to  > Attachments 选 ...