文章目录
1. 字符指针变量
在指针的类型中我们知道有⼀种指针类型为字符指针 char* ;
一般使用于:指向单个字符变量
int main()
{
char ch = 'w';
char *pc = &ch;
*pc = 'w';
return 0;
}
当然还有一个使用:指向字符串变量
int main()
{
const char* pstr = "hello bit.";//这⾥是把⼀个字符串放到pstr指针变量⾥了吗?
printf("%s\n", pstr);
return 0;
}
这里有几点说明:
- const char* pstr = “hello bit.” 并不是把⼀个字符串放到pstr指针变量,而是字符串 hello bit . 首字符的地址放到了 pstr 中
- const char* pstr = “hello bit.”中的 “hello bit.”是一个常量字符串,是不可以直接或者通过指针修改的:
所以在指向常量字符串的指针通常都要const修饰
#include<stdio.h>
int main()
{
const char* pstr = "hello bit.";
pstr = 'c';
printf("%s\n", pstr);
return 0;
}
- 字符串其实就是字符数组,可以使用访问数组的元素的方式访问字符串
#include<stdio.h>
int main()
{
char* pstr = "hello bit.";
printf("%c\n", "hello bit."[0]);
printf("%c\n", "hello bit."[1]);
printf("%c\n", "hello bit."[2]);
printf("%c\n", "hello bit."[3]);
return 0;
}
《剑指offer》中收录了⼀道和字符串相关的笔试题,进一步说明字符指针:
#include <stdio.h>
int main()
{
char str1[] = "hello bit.";
char str1[] = "hello bit.";
const char *str3 = "hello bit.";
const char *str4 = "hello bit.";
if(str1 ==str2)
printf("str1 and str2 are same\n");
else
printf("str1 and str2 are not same\n");
if(str3 ==str4)
printf("str3 and str4 are same\n");
else
printf("str3 and str4 are not same\n");
return 0;
}
- str1 与 str2 都是字符数组,开辟的是不同地址的内存,str1 ==str2比较的是地址,所以str1 and str2 are not same
- str3和str4指向的是⼀个同⼀个常量字符串。C/C++会把常量字符串存储到单独的⼀个内存区域,当⼏个指针指向同⼀个常量字符串的时候,他们实际会指向同⼀块内存,所以str3 and str4 are same。
2. 数组指针变量
2.1 数组指针是什么,指针数组又是什么?
总结:数组指针变量 是一个指向数组的指针,指针数组变量是 一个存储指针的数组
示例:
int *p1[10];//指针数组
int (*p2)[10];//数组指针
- int *p1[10]中, p1 先与 [10] 结合,声明其是一个数组,在与 int * 结合,声明其里面的元素类型为 int * ,所以为指针数组。
- int ( * p2)[10]中,p先和*结合,说明p是⼀个指针变量变量,然后指着指向的是⼀个⼤⼩为10个整型的数组。所以p是⼀个指针,指向⼀个数组,叫 数组指针。
- 这⾥要注意: [] 的优先级要⾼于 * 号的,所以必须加上 () 来保证 p 先和 * 结合。
2.2 数组指针变量怎么初始化
语法;
int arr[10] = {0};
int(*p)[10] = &arr;
数组指针类型解析:
int (*p) [10] = &arr;
| | |
| | |
| | p指向数组的元素个数
| p是数组指针变量名
p指向的数组的元素类型
也就是
int (*)[];
3. ⼆维数组传参的本质
有了数组指针的理解,我们就能够讲⼀下⼆维数组传参的本质了。
过去我们有⼀个⼆维数组的需要传参给⼀个函数的时候,我们是这样写的:
#include <stdio.h>
void test(int a[3][5], int r, int c)
{
int i = 0;
int j = 0;
for(i=0; i<r; i++)
{
for(j=0; j<c; j++)
{
printf("%d ", a[i][j]);
}
printf("\n");
}
}
int main()
{
int arr[3][5] = {{1,2,3,4,5}, {2,3,4,5,6},{3,4,5,6,7}};
test(arr, 3, 5);
return 0;
}
这⾥实参是⼆维数组,形参也写成⼆维数组的形式。
通过数组指针,我们又一个写法
#include <stdio.h>
void test(int (*p)[5], int r, int c)
{
int i = 0;
int j = 0;
for(i=0; i<r; i++)
{
for(j=0; j<c; j++)
{
printf("%d ", *(*(p+i)+j));
}
printf("\n");
}
}
int main()
{
int arr[3][5] = {{1,2,3,4,5}, {2,3,4,5,6},{3,4,5,6,7}};
test(arr, 3, 5);
return 0;
}
说明:
⾸先我们再次理解⼀下⼆维数组::二维数组的每个元素是⼀个一维数组,二维数组的首元素就是第一行,是个一维数组地址。
如下图:
第⼀⾏的⼀维数组的类型就是 int [5] ,所以第⼀⾏的地址的类型就是数组指针类型 int(*)[5],那么形参也是可以写成指针形式的。int (*p)[5]这样的写法,就把第一行这个一维数组的地址传入p中,让p指向arr的行地址,这样就起到了二维数组的作用
在 *( *(p+i)+j)中, p+i 指向了第 i 行的数组的地址,再 * (p+i) 得到 第 i行数组的首元素地址 , 再 * (p+i)+j 得到第 i 行数组的 第 j 列元素的地址 ,在解引用得到 i 行 j 列的元素
4. 函数指针变量
根据前面的知识,我们知道数组指针是指向数组的指针,我们不难得出结论,那么函数指针,其实也就是指向函数的指针
4.1 函数指针变量的创建
首先来解决一个疑问:既然指针实质上指向的是地址,那么函数是否有地址
#include <stdio.h>
void test()
{
printf("hehe\n");
}
int main()
{
printf("test: %p\n", test);
printf("&test: %p\n", &test);
return 0;
}
输出结果如下:
test: 005913CA
&test: 005913CA
确实打印出来了地址,所以函数是有地址的,函数名就是函数的地址,当然也可以通过 &函数名 的⽅式获得函数的地址。
如果我们要将函数的地址存放起来,就得创建函数指针变量,函数指针变量的写法其实和数组指针⾮常类似。如下:
void test()
{
printf("hehe\n");
}
void (*pf1)() = &test;
void (*pf2)()= test;
int Add(int x, int y)
{
return x+y;
}
int(*pf3)(int, int) = Add;
int(*pf3)(int x, int y) = &Add;//x和y写上或者省略都是可以的
函数指针类型解析:
int (*pf3) (int x, int y)
| | ------------
| | |
| | pf3指向函数的参数类型和个数的交代
| 函数指针变量名
pf3指向函数的返回类型
也就是
int (*) (int , int ) //pf3函数指针变量的类型
注意:如果*pf3不用()的话会变成什么?
int * pf3 (int x, int y)
()会与pf3先结合,变成 接受两个int 类型的参数,返回一个int * 类型的函数,在使用函数指针时,要注意把*pf3括起来
4.2 函数指针变量的使⽤
通过函数指针调⽤指针指向的函数。
#include <stdio.h>
int Add(int x, int y)
{
return x+y;
}
int main()
{
int(*pf3)(int, int) = Add;
printf("%d\n", (*pf3)(2, 3));
printf("%d\n", pf3(3, 5));
return 0;
}
输出结果:
5
8
pf3 与 (*pf3) 的结果都是Add的地址,所以在调用函数指针时,这两种写法都可以。
4.3 两段有趣的代码(重点,本文内容讲述的实践)
- 如何解释这段代码?
(*(void (*)())0)();
- void (*)() 是一个函数指针类型,该函数不接受参数,返回一个 void * 类型
- (void (*)())0 是int类型的 0 这个数强制类型转换为 一个函数的地址,0此时就为一个函数的地址
- 先把(void (*)())0 = pf3(看作),原式= ( * pf3 ) ( )= pf3 ( )
- 本质上我们可以看出,该代码为调用 0 地址处的函数
- 那又如何解释这段代码?
void (*signal(int , void(*)(int)))(int);
- signal(int , void(*)(int)) 是函数声明的一段(缺少返回值)
- 其第一个参数为 int ,第二个参数为一个函数指针类型 , 这个函数指针的参数为 int类型,返回类型为一个void。
- 除去 signal(int , void(*)(int)) ,原式 = void ( * )(int)能够看得出,这个也是一个函数指针类型,与signal的第二个参数一致。
- 在函数指针类型中,void ( * )(int) * 后面的位置是这个函数指针的变量名,如果一个函数的返回类型是一个函数指针类型,函数名包括函数参数都写在* 后
- 所以上述代码就一个叫signal函数的函数声明,其接受 一个int 和一个函数指针类型 的参数 ,返回值类型为一个函数类型指针
- typedef 简化:能够清晰的看出
typedef void(*pft)(int);
pft signal(int, pft);
4.3.1 typedef关键字
typedef 是⽤来类型重命名的,可以将复杂的类型,简单化。
typedef unsigned int ui;
//将unsigned int 重命名为ui
如果是指针类型,能否重命名呢?其实也是可以的,⽐如,将 int* 重命名为 ptr_t ,这样写:
typedef int* ptr_t;
但是对于数组指针和函数指针稍微有点区别:
⽐如我们有数组指针类型 int(*)[5] ,需要重命名为 parr_t ,那可以这样写:
typedef int(*parr_t)[5]; //新的类型名必须在*的右边
函数指针类型的重命名也是⼀样的,⽐如,将 void(*)(int) 类型重命名为 pf_t ,就可以这样写:
typedef void(*pfun_t)(int);//新的类型名必须在*的右边
那么要简化4.3的第二段代码就可以这样:
typedef void(*pft)(int);
pft signal(int, pft);
5. 函数指针数组
数组是⼀个存放相同类型数据的存储空间,我们已经学习了指针数组,⽐如:
int *arr[10];
//数组的每个元素是int*
那要把函数的地址存到⼀个数组中,那这个数组就叫函数指针数组。
声明语法:
int (*parr1[3])();
| | | |
| | | 形参位置
| | 数组的成员个数
| 变量名称
返回类型
parr1 先和 [] 结合,说明 parr1是数组,数组的内容是 int (*)() 类型的函数指针。
6. 转移表
函数指针数组的⽤途:转移表
计算器的⼀般实现:
#include <stdio.h>
int add(int a, int b)
{
return a + b;
}
int sub(int a, int b)
{
return a - b;
}
int mul(int a, int b)
{
return a * b;
}
int div(int a, int b)
{
return a / b;
}
int main()
{
int x, y;
int input = 1;
int ret = 0;
do
{
printf("*************************\n");
printf(" 1:add 2:sub \n");
printf(" 3:mul 4:div \n");
printf(" 0:exit \n");
printf("*************************\n");
printf("请选择:");
scanf("%d", &input);
switch (input)
{
case 1:
printf("输⼊操作数:");
scanf("%d %d", &x, &y);
ret = add(x, y);
printf("ret = %d\n", ret);
break;
case 2:
printf("输⼊操作数:");
scanf("%d %d", &x, &y);
ret = sub(x, y);
printf("ret = %d\n", ret);
break;
case 3:
printf("输⼊操作数:");
scanf("%d %d", &x, &y);
ret = mul(x, y);
printf("ret = %d\n", ret);
break;
case 4:
printf("输⼊操作数:");
scanf("%d %d", &x, &y);
ret = div(x, y);
printf("ret = %d\n", ret);
break;
case 0:
printf("退出程序\n");
break;
default:
printf("选择错误\n");
break;
}
} while (input);
return 0;
}
```通过函数指针数组呈现:
```c
#include <stdio.h>
int
add(int a, int b)
{
return a + b;
}
int sub(int a, int b)
{
return a - b;
}
int mul(int a, int b)
{
return a * b;
}
int div(int a, int b)
{
return a / b;
}
int main()
{
int x, y;
int input = 1;
int ret = 0;
int (*p[5])(int x, int y) = {0, add, sub, mul, div}; // 转移表
do
{
printf("*************************\n");
printf(" 1:add 2:sub \n");
printf(" 3:mul 4:div \n");
printf(" 0:exit \n");
printf("*************************\n");
printf("请选择:");
scanf("%d", &input);
if ((input <= 4 && input >= 1))
{
printf("输⼊操作数:");
scanf("%d %d", &x, &y);
ret = (*p[input])(x, y);
printf("ret = %d\n", ret);
}
else if (input == 0)
{
printf("退出计算器\n");
}
else
{
printf("输⼊有误\n");
}
} while (input);
return 0;
}
本质上就是将函数指针传入数组中,再遍历进行调用。
以上就是本文的全部内容,真的很感谢你能看到这里😘😘!!!!