当前位置:网站首页>Void pointer (void*) usage
Void pointer (void*) usage
2022-06-12 13:50:00 【qq_ forty-two million seven hundred and seventy-five thousand n】
void* Is a special type of pointer , Can be used to store the address of any object .
void *pv =&obj; // obj It can be any type of object
1. As a function parameter
#include <stdio.h>
int void_test(void* data)
{
int num = 0;
num = *(int*)data; // (int*) The role of the data Think of it as a int The pointer ( Cast )
printf("num = %d\n", num);
}
int main()
{
int val;
val = 123;
void_test(&val);
return 0;
}
// Compile and run the above code , The output is :
// num = 123
2.void Plus one operation of pointer
stay ANSI The following code in is wrong
void * pvoid;
pvoid++; //ANSI: error
pvoid += 1; //ANSI: error
GNU Appoint void * The algorithm operation and char * Agreement
void * pvoid;
pvoid++; //GNU: correct
pvoid += 1; //GNU: correct
To cater to ANSI standard , And improve the portability of the program , We can write code that does the same thing :
void * pvoid;
(char *)pvoid++; //ANSI: correct ;GNU: correct
(char *)pvoid += 1; //ANSI: error ;GNU: correct
GNU and ANSI There are also some differences , Overall speaking ,GNU a ANSI more “ to open up ”, Provides support for more Syntax . But when we're actually designing , Or should we try to cater to ANSI standard .
other
If the argument to a function can be a pointer of any type , Then it should be declared that the parameter is void *
Typical examples are memory operation functions memcpy and memset The function prototypes of are :
void * memcpy(void *dest, const void *src, size_t len);
void * memset ( void * buffer, int c, size_t num );
Reference resources :https://www.cnblogs.com/geekham/p/4225993.html
边栏推荐
- Install RPM package offline using yum
- Recursion of subviews of view
- Factory mode of "object creation" mode
- The problem of Joseph in Informatics
- Codeforces 1629 A. download more RAM - simple greed
- 颜色编码格式介绍
- 上海解封背后,这群开发者“云聚会”造了个AI抗疫机器人
- 播放器屏幕方向方案
- Codeforces 1629 C. Mexico array - simple greed
- Data type conversion and conditional control statements
猜你喜欢

Pytorch to onnx, onnxruntime reasoning in mmclas

高通平台开发系列讲解(协议篇)QMI简单介绍及使用方法

Create a small root heap and judge the node relationship (also.C\u str() substr(),atoi(),string. Use of find())

阿里云开发板HaaS510报送设备属性

阿里云开发板HaaS510响应UART串口指令

Is MySQL query limit 1000,10 as fast as limit 10? How to crack deep paging

Qualcomm platform development series (Protocol) QMI brief introduction and usage

Transmission and response of events and use cases

Web3.0,「激发创造」的时代

阿里云开发板HaaS510将串口获取数据发送到物联网平台
随机推荐
Computational hierarchy -- the problem of large numbers multiplying decimals
618 entered the second half of the period, apple occupied the high-end market, and the domestic mobile phones finally undercut the price competition
Debug code to quickly locate the error location
2062: [example 1.3] movie tickets
Xcode debugging OpenGLES
Teach you how to create SSM project structure in idea
Codeforces 1638 A. reverse - simple thinking
2065: [example 2.2] sum of integers
Acwing: topology sequence
static 和 extern 关键字详解
数据类型转换和条件控制语句
Symbolic constant, const qualifier
MySQL 查询 limit 1000,10 和 limit 10 速度一样快吗? 深度分页如何破解
Alibaba cloud development board haas510 responds to UART serial port instructions
十四周作业
Application of binary search -- finding the square root sqrt of a number
一种快速创建测试窗口的方法
字节序数据读写
Data type conversion and conditional control statements
Is MySQL query limit 1000,10 as fast as limit 10? How to crack deep paging