有结构体定义如下:
typedef struct
char m_title[20];
char m_author[20];
int m_pages;
void(*m_Printlnfo)();
BookType;
定义两个不同的打印函数 voidPrintlnfo1()和 void
Printlnfo2()。
main函数中可以定义结构体变量:
BookType book1, book2;并对book1,book2赋值,将Printlnfo1和Printlnfo2分别赋值给book1和book2。
并在main中调用
book1.m_Printlnfo();
book2.m_Printlnfo();
#include <stdio.h>
typedef struct
{
char m_title[20];
char m_author[20];
int m_pages;
void (*m_PrintInfo)();
} BookType;
void PrintInfo1()
{
printf("PrintInfo1\n");
}
void PrintInfo2()
{
printf("PrintInfo2\n");
}
int main()
{
BookType book1 = {"Book1", "Author1", 123, PrintInfo1};
BookType book2 = {"Book2", "Author2", 456, PrintInfo2};
book1.m_PrintInfo();
book2.m_PrintInfo();
return 0;
}