c指针数组与数组指针与指针函数与函数指针笔记

#include <stdio.h>
#include <stdlib.h>


typedef struct t_ooxx
{
char *ip;
int w;
int h;
}T_ooxx, *PT_ooxx;


//测试数组指针 
void test_ooxx_arrayP(T_ooxx (*p)[], int num)
{
printf("%s--%d--%d\n", (*p)[0].ip, (*p)[0].w, (*p)[0].h);
}


//测试指针数组 
void test_ooxx_Parray(T_ooxx **p, int num)
{
printf("%s--%d--%d\n", p[0]->ip, p[0]->w, p[0]->h);
}


//测试函数指针 
char *test_ooxxFP(void)
{
char *ooxx = "fuck";
printf("ooxx is what?\n");

return ooxx;



//测试指针函数变量 


double (*test_ooxxPF)(T_ooxx *p, int num);  //定义一个指针函数变量 


typedef  double (*test_ooxxPF_type)(T_ooxx *p, int num); //声明指针函数类型 
 


/* 定义一个与指针函数相同形式的测试函数(一般为处理函数)  */ 
double fuck_fHANDLE(T_ooxx *p, int num)
{
printf("this is a fuck function:%s-%d-%d\n", p->ip, p->w, p->h);
return 0; 



//测试指针函数作为参数 
//void * test_ooxx(double (*p)(T_ooxx *ooxx, int num) , int num)
void * test_ooxx_pm(test_ooxxPF_type p, int num) //也可以这样使用 声明的指针函数类型 
{
T_ooxx ooxx = {"192.168.0.0",456,678};
p(&ooxx, 9); //草做,执行函数 
printf("testing parameter is PF.\n");



int main()
{
//int a[10] = {1,2,3,4,5,6,7,8,9,10};
char *ip = "192.168.1.1";


/* 1 数组指针 */
T_ooxx  (*ooxx)[9] = NULL;

/* if use malloc, sample...*/
ooxx = (T_ooxx (*)[])malloc( 1 * sizeof(T_ooxx)); //指针使用前,要确保指向合法地址 
free(ooxx);

/* 一维数组,与二维数组的测试 */ 
#if 1
T_ooxx testAP[][9] = {
{
"192.168.1.110",123,321,
"192.168.1.119",222,333               //分组初始化 
//.....还有7个 
}
};

ooxx =  testAP; //即&testAP[0]
#else


T_ooxx testAP[9]; //testAP[3], error 要指向的数组包含9个元素 
testAP[0].ip = ip;
testAP[0].w  = 12;
testAP[0].h  = 34;

ooxx =  &testAP; //取数组的地址 
#endif 


test_ooxx_arrayP(ooxx, sizeof(ooxx) / sizeof(ooxx[0]));


/* 2 指针数组 */ 
T_ooxx  *ooxx2[9] = {0};   
T_ooxx testPA = {
"192.168.1.199",
59,
69
};

ooxx2[0] = &testPA;  //数组里面存放指针的地址 
test_ooxx_Parray(ooxx2, 9);


/* 3 函数指针 */
char *t;
t = test_ooxxFP(); 
printf("the answer is %s\n", t);

/*4 指针函数*/

/*4.1 给指针函数变量赋值 */
test_ooxxPF = fuck_fHANDLE; // or test_ooxxPF = &fuck_fHANDLE;

/*4.2 指针函数变量作为参数*/
test_ooxx_pm(fuck_fHANDLE, 9); // test_ooxx(test_ooxxPF, 9); 

return 0;
}




























































































































评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值