69,335
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <stdbool.h>
#include <memory.h>
#include <stdarg.h>
/*
* Usage: if(eVariable == data1)
*/
typedef enum __gender
{
MALE = 0x01,
FEMAIL = 0x02
}genderTypeDef;
typedef struct __stu
{
char * name;
float score;
genderTypeDef gender;
int number;
void (*test)(struct __stu *thisSample);
}stuTypeDef ;
void print_stu_info(stuTypeDef * pstu)
{
char * s = NULL;
printf("struct size is %d\n", sizeof(stuTypeDef));
printf("student name is: %s\n", pstu->name);
printf("student number is: %d\n", pstu->number);
printf("student score is: %.2f\n", pstu->score);
if( (*pstu).gender == MALE)
s = (char *)"MALE";
else
s = (char *)"FEMALE";
printf("student gender is: %s\n", s);
}
void initia_stu(stuTypeDef *pstu)
{
(*pstu).name = "wangjun";
pstu->gender = MALE;
pstu->number = 1;
pstu->score = 99.99;
}
int main(int argc, char *argv[])
{
stuTypeDef stu ;
initia_stu(&stu);
stu.test = print_stu_info;
stu.test(&stu);
return 0 ;
}
前几天学习这个写的代码
#include <stdio.h>
//几个用于测试的函数
int max(int a, int b)
{
return a>b?a:b;
}
int min(int a, int b)
{
return a<b?a:b;
}
//结构体
struct func
{
int (*max)(int, int);//函数指针
int (*min)(int, int);
};
typedef struct func func; //添加别名
void init(func *data)
{
data->max = max;//初始化函数指针
data->min = min;
}
int main()
{
int a, b;
func test;
init(&test); //初始化,你可以说它是构造函数
a = test.max(100, 215);
b = test.min(64, 42);
printf("result:\nmax: %d\nmin: %d\n", a, b);
return 0;
}
struct a {int (*p)(int );
int d;
};