c语言void指针_C中的void指针

c语言void指针

When we declare a pointer we specify its type which will be same as the type of the variable whose address the pointer will contain. For example if we will declare an integer pointer then it can contain the address of an integer variable only. Take below example.

What if we can have a pointer that can point to any type of variable? This can be done easily using void pointer.

void pointer in C

void pointer is a pointer which is not associated with any data type. It can contain the address of variable of any data type. void pointer is also known as general purpose pointer. We can declare a void pointer in C using void keyword as shown below.

void *p;
int a;
char b;
p=&a;	//valid
p=&b;	//valid


The dereference operator or indirection operator (*) is used for dereferencing a pointer. Dereferencing means accessing the value at the address stored in pointer variable. We have to type caste the pointer variable to dereference it because the void pointer is not associated with any data type. The compiler is unable to find the type of variable pointed by the void pointer. This can be done in following way.

We can’t perform the pointer arithmetic on void pointer. Take below example.

void *p;
int a=20;
p=&a;
p++;	//this will show an error


Lets make one program to understand the concept of void pointer in C.


Output

void pointer in C

This is all about void pointer in C. If you find any mistake or information missing in above tutorial then please mention in the comments.

翻译自: https://www.thecrazyprogrammer.com/2015/02/void-pointer-in-c.html

c语言void指针

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值