void指针(void pointer)

the meaning of void
void is "no type", void is "no type pointer", you can point to any data type

void pointer usage specification
1.void pointer can point to any data type, that is, any data type of pointer can assign to void pointer;
E.G.:
int *pint;
pvoid = pint; //but not pint = pvoid
if you want to assign pvoids to other types of pointer, you need to force type conversions such as:pint = (int*)pvoid

2.in the ANSA C standard,arthmetic operations on pvoid such as pvoid++ or pvoid+=1 are not allowed, but it is legal while in GNU, because GNU thinks void* is the same as char* by default. sizeof(*pvoid) = sizeof(char)

the role of void
2.1 limit the return of the function
2.2 limit the function paremeters
when the function does not need to return a value, you must use void to limit the return value type. for example: void func(int, int)
when the function is not allowed to accept parameters, you must use the void limit,for example: int func(void)

since void pointer can point to any type data, you can assign a void pointer to a any data type pointer,so you can also use the void pointer as a function paremeter, so that the function can accept any data type pointer as a parameter,
E.g:
void* memcpy(void*dest, const void* src, size_t len);

void的含义
void即“无类型”,void *则为“无类型指针”,可以指向任何数据类型。

void指针使用规范
①void指针可以指向任意类型的数据,亦即可用任意数据类型的指针对void指针赋值。例如:
int *pint;
void *pvoid;
pvoid = pint; /* 不过不能 pint = pvoid; */
如果要将pvoid赋给其他类型指针,则需要强制类型转换如:pint = (int *)pvoid;


②在ANSI C标准中,不允许对void指针进行算术运算如pvoid++或pvoid+=1等,而在GNU中则允许,因为在缺省情况下,GNU认为void *与char *一样。sizeof( *pvoid )== sizeof( char ).

void的作用
①对函数返回的限定。
②对函数参数的限定。
当函数不需要返回值时,必须使用void限定。例如: void func(int, int);
当函数不允许接受参数时,必须使用void限定。例如: int func(void)。


由于void指针可以指向任意类型的数据,亦即可用任意数据类型的指针对void指针赋值,因此还可以用void指针来作为函数形参,这样函数就可以接受任意数据类型的指针作为参数。例如:
void * memcpy( void *dest, const void *src, size_t len );
void * memset( void * buffer, int c, size_t num );

转载于:https://www.cnblogs.com/1915884031A-qqcom/p/7674124.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值