兴起凌晨写了个C程序,来猜猜结果~
最近在搞TCP/IP编程,模仿TCP底层代码风格,写了个程序,有些地方其实还挺费解~
大家来猜猜结果,顺便都解释下为什么是这个结果~哈哈
程序代码:
#include <stdio.h> #include <stdlib.h> /* define a pointer function */ typedef void (*out_fn)(struct ip_addr *ip); /* struct ip address */ struct ip_addr{ char* addr; }; /* struct network interface */ struct netif{ struct netif *next; void * data; //void (* out)(struct ip_addr *ip); out_fn out; }; /* a simple function */ void show_ip(struct ip_addr *ip) { printf("ip address:%s \n",ip->addr); } /* main function */ void main() { /* declare and allocation memery */ struct ip_addr *ip=(struct ip_addr*)malloc(sizeof(struct ip_addr)); struct netif *net=(struct netif*)malloc(sizeof(struct netif)); /* show the struct netif size */ printf("struct netif size :%d \n",sizeof net); /* net->data handle */ if(net->data) { net->data="this is network interface data."; printf("%s\n",net->data); } printf("net->data size :%d \n",sizeof net->data); /* pointer function */ if(ip) { ip->addr="192.168.1.110"; net->out=show_ip; net->out(ip); } }
[ 本帖最后由 JaceLin 于 2015-10-2 00:25 编辑 ]