几种指针的区别及用法

来源:互联网 发布:snmpwalk用的什么端口 编辑:程序博客网 时间:2024/06/08 11:38
int *p1[2];
int (*p2)[2];
int (*fptr)(int, int);
int (*fptr2[2])(int, int);

#include <stdio.h>#include <stdlib.h>void test();void test2();void test3();void test4();int add(int a, int b);int sub(int a, int b);typedef int (*fptr)(int, int);typedef int (*fptr2[2])(int, int);int main(){//test();//test2();//test3();test4();return 0;}void test(){int *p1[2];int a1[2][3] = {{1,2,3}, {4,5,6}};p1[0] = &a1[0][0];p1[1] = &a1[1][0];int i;for(i = 0; i<3; i++) {//printf("%d ", *(p1[0]+i));printf("%d ", *(*(p1)+i));}printf("\n");}void test2(){int (*p2)[2];int arr[][2] = {{1,2},{3,4},{5,6}};p2 = arr;int i;for(i=0; i<2; i++) {printf("%d ", *(p2[0]+i));}printf("\n");}void test3(){fptr f = add;printf("%d\n", f(3,4));}void test4(){fptr2 f;f[0] = add;f[1] = sub;printf("%d %d\n", f[0](1,2), f[1](1,2));}int add(int a, int b){return a+b;}int sub(int a, int b) {return a-b;}




0 0