stm32语言c指针吗,STM32——C语言知识点:指针、结构体

1 /*

2 ============================================================================3 Name : Cyuyanfuxi.c4 Author :5 Version :6 Copyright : Your copyright notice7 Description : Hello World in C, Ansi-style8 ============================================================================9 */

10

11 #include

12 #include

13 //函数声明

14 void reset(inti);15 void reset2(int*p);16 void add_by_point(int x,int y,int *result);17 //结构体定义

18 structstudent19 {20 intage;21 floatweight;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个,那么数组内容默认为046 //结论:数组名也是数字首地址

47 printf("%d\n",code);//求数组名的地址 2686732 发现一样的

48 printf("%d\n",&code[0]);//求数组的第一个数字的地址 268673249 //指针运算要根据指针的类型

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);//063 //什么是返回值

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的地址268669278 //结构体指针运算根据指针的类型来判断

79 printf("%d\n",(&kinson+1));//2686692+28=268672080 //结构体的地址就是第一个成员的地址

81 printf("%d\n",&kinson.age);//268669282 //结构体成员的地址是连续的

83 printf("%d\n",&kinson.weight);//2686696

84 printf("%d\n",&kinson.name);//268670085

86 //printf("%d\n",kinson.name);

87 puts("!!!Hello World!!!"); /*prints !!!Hello World!!!*/

88

89 returnEXIT_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 returnq;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 }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值