当前位置:网站首页>Pointer function of C language
Pointer function of C language
2022-07-19 05:30:00 【Learn to put down ta】
Variable parameter function
Variable parameter functions require specific header files , There's a specific format , Please look at the following example :
#include <stdio.h>
#include <stdarg.h>// First, reference a specific header file
// Write a sum function
int sum(int n,...);// Statement & Definition n Indicates the number of parameters ... Indicates that the parameters are indefinite
int sum(int n,...)
{
int i,sum=0;
va_list vap;// Load parameter list
va_start(vap,n);// Open the parameter list
for(i=0;i<n;i++)
{
sum+=va_arg(vap,int);// Using parameter
}
va_end(vap);// Close the parameter list
return sum;// Return value
}
Pointer function
A pointer function is a function that returns a pointer
for instance :
char* xxx(int a)
{
if(a==1)
return "just do it!";// The address of the first character of the string is returned
...
}
Be careful : Cannot return pointer to local variable
A function pointer
Is a pointer to a function , It is similar to the relationship between pointer array and array pointer
int square(int a)
{
return a*a;
}
int (* xxx)(int x);// Define a function pointer xxx Point to one, there is one int Function of parameter
xxx = □// to xxx initialization
(*xxx)(2);// Call function through function pointer , Or you could write it as xxx(2), But in order to distinguish from the function name , Bracketed and *
Function pointer as function parameter
int add(int num1,int num2)
{
return (num1+num2);
}
int a(int (*b)(int , int),int num1,int num2)// Define a function , Three parameters ( Look at the comma ), One parameter is the function pointer , The return value of the function pointed to is int, There are two int Parameters , The other two parameters are int
{
return (*b)(num1,num2);
}
a(add,1,3);// Then call directly
Take the function pointer as the return value
int (*select(char))(int num1,int num2);
according to C Language From left to right, from inside to outside Analyze this definition in the order of :select(char) First of all, it is confirmed that this is a named select Function of , There is one char Parameters , The rest is int (*)(int num1,int num2) Return value : A function pointer , There are two points int Function of parameter
边栏推荐
猜你喜欢

redis 源码分析 动态字符串实现(sds)

ambari 2.7.5集成安装hue 4.6

聊聊并发编程的12种业务场景

Online software testing training institutions lemon class and itest AI platform achieves strategic cooperation

操作系統常見面試題

Excel imports long data and changes to 000 at the end

9.数据仓库搭建之DIM层搭建

Mapbox loads local offline terrain

MySQL lock

基于PaddleOCR解决文本检测训练模型与inference模型预测效果不一致的问题
随机推荐
函数与参数
10问10答:你真的了解线程池吗?
MySQL安装配置教程(超级详细)
Router loopback port experiment
Network command: network card information, netstat, ARP
C语言文件的操作
用Flink SQL流化市场数据2:盘中风险价值
聊聊redis分布式锁的8大坑
Two or three things to know about operation and maintenance safety
使用Flink SQL传输市场数据1:传输VWAP
Face scum counter attack: thread pool lethal serial eighteen questions, the interviewer praised me straight
[AI] action recognition using simple neural network -- Based on coco key points
MySQL cache solution problem solving
Redis 源码分析-数据结构及实现(字典dict)
从20s优化到500ms,我用了这三招
Easypoi之excel模板导出
Swagger configuration and use
Redis source code analysis skip table implementation
idea导入本地包
Web3js development technology