1,数组指针语法梳理
回顾,如何定义数组数据类型:
回顾,如何定义指针类型数组:
回顾,如何直接定义 一个指向数组类型的指针:
2,函数指针语法梳理
1)如何定义一个函数类型
2)如何定义一个函数指针类型
3)如何定义一个函数指针(指向一个函数的入口地址)
【中级程序员 转 高级程序员的 必经之路】
1,函数类型做函数的参数
把函数的入口地址传过来,奇怪的效果:【多态就是这样】
函数指针 做 函数参数 思想剖析
1,数组指针语法梳理
回顾,如何定义数组数据类型:
chunli@http://990487026.blog.51cto.com~/c++$ cat main.cpp
#include <iostream>
using namespace std;
int main()
{
typedef int (arr) [5]; //定义一种数据类型
arr myarr; //用数据类型,定义变量
myarr[3] = 10; //使用
cout << myarr[3] << endl;
int a[5]; //等同于这样写
return 0;
}
chunli@http://990487026.blog.51cto.com~/c++$ g++ -g -o run main.cpp && ./run
10
chunli@http://990487026.blog.51cto.com~/c++$
回顾,如何定义指针类型数组:
chunli@http://990487026.blog.51cto.com~/c++$ cat main.cpp
#include <iostream>
using namespace std;
int main()
{
typedef int (*arr) [10]; //定义一种数据类型
arr my_arr; //用数据类型申明一个变量
int a[10]; //常规的数组
my_arr = &a; //区地址给我
(*my_arr)[0] = 9999;
cout << *((*my_arr) +0) << endl;
return 0;
}
chunli@http://990487026.blog.51cto.com~/c++$ g++ -g -o run main.cpp && ./run
9999
chunli@http://990487026.blog.51cto.com~/c++$
回顾,如何直接定义 一个指向数组类型的指针:
chunli@http://990487026.blog.51cto.com~/c++$ cat main.cpp
#include <iostream>
using namespace std;
int main()
{
int arr[10];
int (*p)[10];
p = &arr;
(*p)[2] = 10;
cout << *(*(p) + 2)<< endl;
return 0;
}
chunli@http://990487026.blog.51cto.com~/c++$ g++ -g -o run main.cpp && ./run
10
chunli@http://990487026.blog.51cto.com~/c++$
2,函数指针语法梳理
1)如何定义一个函数类型
2)如何定义一个函数指针类型
3)如何定义一个函数指针(指向一个函数的入口地址)
1,函数的基本调用:
chunli@http://990487026.blog.51cto.com~/c++$ cat main.cpp
#include <iostream>
using namespace std;
int add(int a,int b)
{
return a + b;
}
int main()
{
int sum = add(1,2); //函数名,就是函数的入口地址
cout << sum <<endl;
return 0;
}
chunli@http://990487026.blog.51cto.com~/c++$ g++ -g -o run main.cpp && ./run
3
chunli@http://990487026.blog.51cto.com~/c++$
1)如何定义一个函数类型
使用函数指针,间接调用函数
chunli@http://990487026.blog.51cto.com~/c++$ cat main.cpp
#include <iostream>
using namespace std;
int add(int a,int b)
{
return a + b;
}
int main()
{
typedef int (fun)(int a,int b); //定义一种fun的函数类型
fun *p = NULL; //定义一个变量
p = &add; //把返回值,参数类型完全一样的函数地址,给我
p = add; //这样写,兼容C历史
int sum = p(1,6);
cout << sum <<endl;
return 0;
}
chunli@http://990487026.blog.51cto.com~/c++$ g++ -g -o run main.cpp && ./run
7
chunli@http://990487026.blog.51cto.com~/c++$
2)如何定义一个函数指针类型
chunli@http://990487026.blog.51cto.com~/c++$ cat main.cpp
#include <iostream>
using namespace std;
int add(int a,int b)
{
return a + b;
}
int main()
{
typedef int (*fun)(int a,int b);//定义一种fun的函数类型
fun p = NULL; //定义一个变量
p = add; //这样写,兼容C历史
int sum = p(1,6);
cout << sum <<endl;
return 0;
}
chunli@http://990487026.blog.51cto.com~/c++$ g++ -g -o run main.cpp && ./run
7
chunli@http://990487026.blog.51cto.com~/c++$
3)如何定义一个函数指针(指向一个函数的入口地址)
chunli@http://990487026.blog.51cto.com~/c++$ cat main.cpp
#include <iostream>
using namespace std;
int add(int a,int b)
{
return a + b;
}
int main()
{
int (*fun)(int a,int b); //定义一个变量
fun = add;
int sum = fun(1,6);
cout << sum <<endl;
return 0;
}
chunli@http://990487026.blog.51cto.com~/c++$ g++ -g -o run main.cpp && ./run
7
chunli@http://990487026.blog.51cto.com~/c++$
【中级程序员 转 高级程序员的 必经之路】
1,函数类型做函数的参数
准备:
chunli@http://990487026.blog.51cto.com~/c++$ cat main.cpp
#include <iostream>
using namespace std;
typedef int (*fun)(int a,int b); //定义一种数据类型
int add(int a,int b)
{
return a + b;
}
int main()
{
fun f = NULL; //使用数据类型,定义一个变量
f = &add; //给变变量赋值
cout << f(1,2) << endl;
return 0;
}
chunli@http://990487026.blog.51cto.com~/c++$ g++ -g -o run main.cpp && ./run
3
chunli@http://990487026.blog.51cto.com~/c++$
chunli@http://990487026.blog.51cto.com~/c++$ cat main.cpp
#include <iostream>
using namespace std;
typedef int (*fun)(int a,int b); //定义一种数据类型
int add(int a,int b)
{
return a + b;
}
//下面的这两种方式的是一样的
int chunli_1(fun f)
{
return f(3,5);
}
int chunli_2( int (*fun)(int a,int b))
{
return fun(2,3);
}
int main()
{
cout << chunli_1(add)<< endl;
cout << chunli_2(add)<< endl;
return 0;
}
chunli@http://990487026.blog.51cto.com~/c++$ !g
g++ -g -o run main.cpp && ./run
8
5
chunli@http://990487026.blog.51cto.com~/c++$
把函数的入口地址传过来,奇怪的效果:【多态就是这样】
chunli@http://990487026.blog.51cto.com~/c++$ cat main.cpp
#include <iostream>
using namespace std;
typedef int (*fun)(int a,int b); //定义一种数据类型
int add1(int a,int b)
{
cout << "add1 函数被调用 \n";
return a + b;
}
int add2(int a,int b)
{
cout << "add2 函数被调用 \n";
return a + b;
}
int add3(int a,int b)
{
cout << "add3 函数被调用\n";
return a + b;
}
int add4(int a,int b)
{
cout << "add4 函数被调用\n";
return a + b;
}
//下面的这两种方式的是一样的
int chunli_1(fun f)
{
return f(3,5);
}
int chunli_2( int (*fun)(int a,int b))
{
return fun(2,3);
}
int main()
{
cout << chunli_1(add1)<< endl;
cout << chunli_2(add2)<< endl;
cout << chunli_2(add3)<< endl;
cout << chunli_2(add4)<< endl;
return 0;
}
chunli@http://990487026.blog.51cto.com~/c++$ g++ -g -o run main.cpp && ./run
add1 函数被调用
8
add2 函数被调用
5
add3 函数被调用
5
add4 函数被调用
5
chunli@http://990487026.blog.51cto.com~/c++$
函数指针 做 函数参数 思想剖析
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。