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.

转载于:https://www.cnblogs.com/passedbylove/p/11208641.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
比较简短的C语言教程,英文资料 1.0 Introduction 1.1 The main() Function 1.2 Include Files 1.3 Whitespace 1.4 The Preprocessor 1.4.1 The #define Directive 1.4.2 Comments 1.4.3 The #include Directive 1.4.4 The #ifdef/#else/#endif Directives 1.4.5 More Directives 1.4.6 Predefined Macros 2.0 Basic Data Types 2.1 Signed Integer Types 2.2 Unsigned Integer Types 2.3 Integer Overflow 2.4 Real Data Types 2.5 BCC32 Implementation 2.6 The void Type 3.0 Control Flow 3.1 The if/else/endif Statements 3.2 Compound Statements 3.3 Nested if Statements 3.4 The switch/case Statement 3.5 The for Statement 3.6 The while Statement 3.7 The do/while Statement 3.8 The break Statement 3.9 The continue Statement 3.10 The goto Statement 3.11 The return Statement 4.0 Expressions and Operators 4.1 Basic Arithmetic Operators 4.2 Promotion and Casting 4.3 More Arithmetic Operators 4.4 Assignment Operators 4.5 Bitwise Operators 4.6 Relational Operators 4.7 Logical Operators 4.8 The Conditional Operator The C Cheat Sheet Revision 1 Copyright ? 2000 Andrew Sterian iv 4.9 The Comma Operator 4.10 Operator Precedence and Association 5.0 Program Structure 5.1 Declaring Functions 5.2 Calling Functions 5.3 Local Variables 5.4 Global Variables 6.0 Advanced Data Types 6.1 Arrays 6.2 Structures 6.3 Pointers 6.4 Strings 6.4.1 String Pointers 6.4.2 String Operations 6.5 Enumerated Types 6.6 Bitfields 6.7 Unions 7.0 Advanced Programming Concepts 7.1 Standard I/O 7.1.1 Opening Files 7.1.2 Writing to File Pointers 7.1.3 Reading from File Pointers 7.1.4 The stdin/stdout/stderr Streams 7.2 Dynamic Memory Allocation 7.3 Memory Manipulation 7.4 Declaring New Types 7.5 Pointers to Functions 7.6 Command-Line Parameters 8.0 Multi-File Programs 8.1 Basic Concepts 8.2 Include Files as Interfaces 8.3 Object Files and Linking 8.4 The Details of the Compilation Process 9.0 The Standard C Library 9.1 Assertion Checking 9.2 Character Classification 9.3 Error Reporting 9.4 Buffer Manipulation 9.5 Non-Local Jumps 9.6 Event Signalling 9.7 Variable-Length Argument Lists 9.8 Miscellaneous Functions 9.9 String Handling 9.10 Time Functions 9.11 Floating-Point Math 9.12 Standard I/O 10.0 Tips, Tricks, and Caveats 10.1 Infinite Loops 10.2 Unallocated Storage 10.3 The Null Statement 10.4 Extraneous Semicolons 10.5 strcmp is Backwards 10.6 Unterminated Comments 10.7 Equality and Assignment 10.8 Assertion Checking 10.9 Error Checking 10.10 Programming Style 11.0 Differences between Java and C

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值