STM32——C語言知識點:指針、結構體


  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 }

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM