结构体的基本操作

 

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include<stdio.h>
 3 #include<stdlib.h>
 4 #include<string.h>
 5 
 6 //定义一个结构体
 7 //定义一个数据类型。固定内存大小的别名,还没有分配内存
 8 /*struct Teacher
 9 {
10     char name[5];
11     int age;
12 };*/
13 typedef struct Teacher
14 {
15     char name[64];
16     int age;
17     int id;
18 }Teacher;
19 
20 
21 struct Student
22 {
23     char name[64];
24     int age;
25 }s1,s2;//定义类型 同时定义变量
26 
27 struct
28 {
29     char name[64];
30     int age;
31 
32 }s3, s4;    //匿名类型 定义变量
33 
34 //初始化变量的三种方法
35 //定义变量 然后初始化
36 //
37 Teacher t7 = { "aaaaa", 18, 01 };    //全局
38 struct Student2
39 {
40     char name[64];
41     int age;
42 }s5 = { "names", 21 };
43 
44 struct
45 {
46     char name[64];
47     int age;
48 
49 }s6 = { "names", 30 };
50 int main()
51 {
52     //struct  Teacher t1;    //告诉C编译器给我分配内存
53     Teacher t1;
54     Teacher t2 = { "aaaaa", 18, 01 };    //定义变量 然后初始化
55 
56     t1.age = 32;    //t1. 的.是寻址操作 计算t1相对于t1大变量的     
57     //偏移量 ===》计算在cpu中进行,没有操作内存
58 
59     //通过指针的方式 操作 内存空间
60     {
61         Teacher *p = NULL;
62         p = &t2;
63         printf("p->age:%d\n", p->age);    //-> 是寻址操作 相对于t2大变量的
64                                         //偏移量 ===》计算在cpu中进行,没有操作内存
65         printf("p->name:%s\n", p -> name);
66     }
67     strcpy(t1.name, "张三");
68     printf("ti.name%s\n", t1.name);
69     system("pause");
70     return 0;
71 }

 

posted @ 2015-10-10 17:07  微风星宇  阅读(674)  评论(0编辑  收藏  举报