怎么做博客网站,网页设计与制作教程清华大学出版社,seo 网站推广,营销网站的基本要素前面的入门中已经介绍了指针的基础知识#xff0c;接下来#xff0c;让我们继续学习吧#xff01;
一. 字符指针变量
char*
一般形式
int main()
{char n w;char* pa n;*pa w;return 0;
} 这并不是把字符串hello world放在n中#xff0c;而是把第一个字符的地址…前面的入门中已经介绍了指针的基础知识接下来让我们继续学习吧
一. 字符指针变量
char*
一般形式
int main()
{char n w;char* pa n;*pa w;return 0;
} 这并不是把字符串hello world放在n中而是把第一个字符的地址放在n中
int main()
{char str1[] hello world;char str2[] hello world;const char* str3 hello world;const char* str4 hello world;if (str1 str2)printf(str1 and str2 are same\n);elseprintf(str1 and str2 are not same\n);if (str3 str4)printf(str3 and str4 are same\n);elseprintf(str3 and str4 are not same\n);return 0;
} 这是因为str1和str2是数组因此表示的是首元素的地址这两个不同。
str3和str4是常量字符串内容相同的常量字符串只会保存一份 二.数组指针变量
int * n;
float * a;
int *p1[10];p1是数组10个元素每个元素的类型是int*指针数组
int *p2[10]p2是指针‘指针指向的是数组10个元素每个元素的类型是int数组指针 三.二维数组传参的本质
void test(int(*p)[5], int r, int c)
{int i 0;int j 0;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;
}
二维数组传参形参可以是数组也可以是指针
void test(int(*p)[5], int r, int c)
void test(int a[3][5], int r, int c)
二维数组是元素是一维数组的数组 四.函数指针变量
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;
int(*pf3)(int x, int y)
int pf3指向函数的返回类型
*pf3 函数指针变量名
int(*)(int x, int y) pf3函数指针变量的类型 五.函数指针数组
int (*parr[3])();
函数指针的类型是int (*)(); 六.回调函数
回调函数是一个通过函数指针调用的函数
也就是说将代码中冗余的部分作为模板然后将它放在一个函数中在下面如果有用到的地方就调用多次调用只是有小部分的不同即可