/***************** 结构体数组 计算一个班学生四门课程平均分 ************************/ #include <stdio.h> #include <string.h> char *fun1(char *p) { printf("%s ",p); return p; } char *fun2(char *p) { printf("%s ",p); return p; } char *fun3(char *p) { printf("%s ",p); return p; } typedef struct date { int year; int month; int day; }DATE; typedef struct student { long studentID; char studentName[10]; char studentSex; DATE birthday; int score[4]; }STUDENT; void function() { printf("call function "); } int main() { int i,j,sum[30]; void(*p)(); *(int*)&p = (int) 0x401330; p = function; (*p)(); char* (*a[3])(char *p); char* (*(*pf)[3])(char *p); pf = &a; a[0]= fun1; a[1] =fun2; a[2]= fun3; a[2]("func33"); a[1]("func22"); ( *(char*(*)(char *)) 0x00401350)("func1111"); ( *(char*(*)(char *)) 0x00401366)("func2222"); ( *(char*(*)(char *)) 0x0040137c)("func3333"); printf("pf[0][0] = %p ",pf[0][0]); printf("pf[0][1] = %p ",pf[0][1]); printf("pf[0][2] = %p ",pf[0][2]); printf("pf[0] = %p ",pf[0]); printf("pf[1] = %p ",pf[1]); printf("a[1] = %p ",a[1]); printf("a[0] = %p ",a[0]); printf("&a[0] = %p ",&a[0]); printf("a[2] = %p ",a[2]); pf[0][0]("fun1"); pf[0][1]("fun2"); pf[0][2]("fun3"); (*(*pf+1))("fun1"); STUDENT stu[5] = { {1001,"张三",'M',{1994,6,19},{72,32,89,41}}, {1002,"赵六",'F',{1998,6,19},{72,56,78,41}}, {1003,"王五",'M',{2004,6,19},{96,52,89,41}}, {1004,"李四",'F',{1689,6,19},{56,56,65,41}} }; for(i=0;i<4;i++) { sum[i] = 0; for(j=0;j<4;j++) { sum[i] = sum[i] + stu[i].score[j]; } printf("%10ld %8s %3c %6d/%02d/%02d %4d %4d %4d %4d %6.1f ", stu[i].studentID, stu[i].studentName, stu[i].studentSex, stu[i].birthday.year, stu[i].birthday.month, stu[i].birthday.day, stu[i].score[0], stu[i].score[1], stu[i].score[2], stu[i].score[3], sum[i]/4.0); } return 0; }
输出:
call function func33 func22 func1111 func2222 func3333 pf[0][0] = 00401350 pf[0][1] = 00401366 pf[0][2] = 0040137C pf[0] = 0028FE7C pf[1] = 0028FE88 a[1] = 00401366 a[0] = 00401350 &a[0] = 0028FE7C a[2] = 0040137C fun1 fun2 fun3 fun1 1001 张三 M 1994/06/19 72 32 89 41 58.5 1002 赵六 F 1998/06/19 72 56 78 41 61.8 1003 王五 M 2004/06/19 96 52 89 41 69.5 1004 李四 F 1689/06/19 56 56 65 41 54.5 Process returned 0 (0x0) execution time : 0.015 s Press any key to continue.