见码如见人:
#include//家庭typedef struct{ char* father; char* mother;} Family;//学生typedef struct{ int age; char* name; char* sex; char* title[2]; Family* family;} Student;int main(int argc, char *argv[]){ //结构体赋值,成员可为空 Student one = {10, "nick", "男",NULL}; Student two = {}; Student* twoPtr = &two; twoPtr->name = "bibi"; printf("%s\n", two.name); //[[-----结构体成员的直接访问 printf("%s\n", one.name); printf("%d\n", one.age); //--------]] // //[[----结构体成员的间接访问 Student* p = &one; printf("one->age %d\n", p->age); printf("one.age %d\n", (*p).age); //--------]] // //[[-----成员为数组时的赋值与读取 one.title[0] = "班长"; printf("%s\n", one.title[0]); //-------]] //[[----成员为结构体时的赋值与读取 // one.family->father = "老张";//不可行,未初始化结构体one.family时不可对成员赋值 // one.family = &(Family){"老张", "老王"};//可行 Family oneFamily = {"老张", "老王"}; one.family = &oneFamily; printf("%s\n", p->family->father); //--------]] return 0;}