【C】C语言结构体指针的语法

时间:2022-09-09 19:48:56
#include <stdio.h> #include <string.h> #include <stdlib.h> struct AGE { int year; int month; int day; }; struct Student { char *name; int num; struct AGE birthday; float score; }; int main() { // 注意写法1与写法2 不能混用 // 写法1 struct Student* stu = NULL; stu = (struct Student *)malloc(sizeof(struct Student)); (*stu).num = 12; printf("%d\n", (*stu).num); // 写法2 struct Student *stu1 = (struct Student *)malloc(sizeof(struct Student)); stu1->name = "jack"; printf("%s\n", stu1->name); return 0; }