先看一段代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct
{
int num;
char name[10];
}A;
typedef struct
{
int a;
float b;
void* c;
}TEST;
int main(void)
{
TEST t = {0};
t.c = (A*)malloc(sizeof(A));
t.a = 10;
t.b = 2.1f;
((A*)(t.c))->num = 10;
strcpy(((A*)(t.c))->name,"zhangsan");
printf("%d\n%f\n%d\n%s\n",t.a,t.b,((A*)(t.c))->num,((A*)(t.c))->name);
return 0;
}
利用结构体中的void*成员,在实现的时候给它赋上特定的值,这样就可以实现不定结构体了。该结构体,不但包含TEST的成员,而且还指向A的成员。