1 /*
2 ============================================================================
3 Name : Cyuyanfuxi.c
4 Author :
5 Version :
6 Copyright : Your copyright notice
7 Description : Hello World in C, Ansi-style
8 ============================================================================
9 */
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 //函数声明
14 void reset(int i);
15 void reset2(int* p);
16 void add_by_point(int x,int y,int *result);
17 //结构体定义
18 struct student
19 {
20 int age;
21 float weight;
22 char name[20];
23 };
24 void struct_caculate(struct student *p);
25
26 int main(void)
27 {
28 //求类型的字节
29 printf("%d\n",sizeof(char));//1字节
30 printf("%d\n",sizeof(int));//4字节
31 printf("%d\n",sizeof(float));//4字节
32 printf("%d\n",sizeof(double));//8个字节输出p1,也就是a的地址
33 puts("1------------------------------------------");
34 int a = 10;//定义一个整型变量a,并赋值为10
35 int *p1 = &a;//定义一个指针*P1,赋值为变量a的地址,
36 char *p2 = p1;//定义一个字符变量p2,赋值为p1的地址
37 printf("%d\n",p1);//输出p1,也就是a的地址(2686776)
38 //运算要根据类型
39 printf("%d\n",p1+1);//输出4210696,也就是2686776+4,因为p1是int类型4字节,所以加4
40 printf("%d\n",*p1);//带*号的意思是输出p1里面的内容,10
41 printf("%d\n",*p2);//10
42 puts("2------------------------------------------");
43 int code[10] = {1 ,2,3,4,5};//定义一个数组
44 //结论:数组内容值默认为0
45 printf("%d\n",code[5]);//输出数组的第5个值,但是数组只有第4个,数组有定义10个,那么数组内容默认为0
46 //结论:数组名也是数字首地址
47 printf("%d\n",code);//求数组名的地址 2686732 发现一样的
48 printf("%d\n",&code[0]);//求数组的第一个数字的地址 2686732
49 //指针运算要根据指针的类型
50 printf("%d\n",code+1);//求数组加一的地址,输出2686732+4
51
52 printf("%d\n",*(code+2));//求数组第三个数字的值,3
53 *(code+2) = 0;//(code+2)是一个地址,*(code+2)是内容,现在把0赋值为里面的内容
54 printf("%d\n",*(code+2));//0
55 puts("3------------------------------------------");
56 int d = 10;
57 reset(d);//函数的调用
58 //结论:函数的独立性,
59 printf("%d\n",d);//10
60 reset2(&d);//取地址
61 //使用指针的方式突破函数壁垒
62 printf("%d\n",d);//0
63 //什么是返回值
64 int e = add(3,5);
65 printf("e = %d\n",e);//8
66 int result = 0;//??
67 //指针的方式计算结果
68 add_by_point(5,5,&result);
69 printf("result = %d\n",result);//10
70 puts("4------------------------------------------");
71 printf("student结构体字节数 = %d\n",sizeof(struct student));//4+4+20=28
72 struct student kinson = //结构体赋值
73 {
74 21,61,"kinson"
75 };
76 printf("%d\n",sizeof(kinson));//28
77 printf("%d\n",&kinson);//取结构体名kinson的地址2686692
78 //结构体指针运算根据指针的类型来判断
79 printf("%d\n",(&kinson+1));//2686692+28=2686720
80 //结构体的地址就是第一个成员的地址
81 printf("%d\n",&kinson.age);//2686692
82 //结构体成员的地址是连续的
83 printf("%d\n",&kinson.weight);//2686696
84 printf("%d\n",&kinson.name);//2686700
85
86 //printf("%d\n",kinson.name);
87 puts("!!!Hello World!!!"); /* prints !!!Hello World!!! */
88
89 return EXIT_SUCCESS;
90 }
91 void reset(int i)//定义一个子函数
92 {
93 i = 0;//赋值i=0;
94 }
95 void reset2(int* p)//定义一个指针函数
96 {
97 *p = 0;//指针p的内容是0
98 }
99
100 int add(int i,int j )//定义一个子函数,什么是返回值要用
101 {
102 /*
103 变量的生命周期
104 */
105
106 int q = i+j;
107 return q;
108 }
109 void add_by_point(int x,int y,int *result)//指针函数要用
110 {
111 int r = (x + y);
112 *result = r;
113 }
114
115 void struct_caculate(struct student *p)
116 {
117
118 p->name = "kinson2";
119
120
121 }
![]()