二级结构体的赋值和访问方法

定义结构体class ,class结构体中包含student 结构体指针

如何访问,赋值给class->student?

#cat struct_find.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct class {
	int num;
	char *name;
	struct student *st;
};

struct student {
	char *name;
	int age;
};

int main(void)
{
	struct class *c1;
	c1 = malloc(sizeof(struct class));
	memset(c1, 0, sizeof(struct class));
	c1->num = 1;
	c1->name = "class1";

	c1->st = malloc(sizeof(struct student));

	c1->st->name = "jack";
	c1->st->age = 22;

	printf("class num:%d, name:%s\n", c1->num, c1->name);
	printf("student name:%s, age:%d\n", c1->st->name, c1->st->age);

	return 0;

}
#./struct_find
class num:1, name:class1
student name:jack, age:22
``
posted @ 2018-01-10 15:24  苏小北1024  阅读(232)  评论(0编辑  收藏  举报