Skip to content

Instantly share code, notes, and snippets.

@muhlenXi
Created August 8, 2020 09:33
Show Gist options
  • Save muhlenXi/27c56de73128a80411f2fac1cc3bbc3c to your computer and use it in GitHub Desktop.
Save muhlenXi/27c56de73128a80411f2fac1cc3bbc3c to your computer and use it in GitHub Desktop.
typedef 和 typedef struct 的使用
#include <stdio.h>
// 给 int 类型起个别名为 INT
typedef int INT;
// 方式 2
// 定义 Student 结构体类型
struct Student {
char name;
int age;
};
// 方式 3
// 定义 HeadTeacher 结构体类型,并声明一个 h1 变量
struct HeadTeacher {
int age;
} h1;
// 方式 4
// 定义一个 结构体类型,并设置别名为 Teacher
typedef struct {
int age;
} Teacher;
// 定义一个 Node 结构体类型,并设置别名为 Node
typedef struct Node {
int val;
struct Node *next;
} Node;
int main(int argc, const char * argv[]) {
// 方式 1
struct {
int age;
} student;
student.age = 18;
printf("student age = %d \n", student.age);
struct Student student1;
student1.age = 20;
printf("student1 age = %d \n", student1.age);
h1.age = 50;
struct HeadTeacher h2;
h2.age = 70;
printf("head teacher1 age = %d \n", h1.age);
printf("head teacher2 age = %d \n", h2.age);
Teacher t;
t.age = 30;
printf("teacher age = %d \n", t.age);
Node n1;
n1.val = 10;
Node n2;
n2.val = 100;
n1.next = &n2;
printf("%d \n", n1.val);
printf("%d \n", n1.next->val);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment