STM32——C语言课堂原代码

指针
1 /* 2 ============================================================================ 3 Name : Hello.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 void reset(int i); 14 void reset2(int* p); 15 void add_by_point(int x,int y,int *result); 16 int main(void) { 17 18 printf("%d\n",sizeof(char)); 19 printf("%d\n",sizeof(int)); 20 int a = 10; 21 int *p1 = &a; 22 char *p2 = p1; 23 24 printf("%d\n",p1); 25 printf("%d\n",p2); 26 27 printf("%d\n",*p1);//10 28 printf("%d\n",*p2); 29 30 puts("----------------------"); 31 int c[10] = { 32 1,2,3,4,5 33 }; 34 //数组内容值默认为0 (一组数组a定义10个变量,前5个分别是1,2,3,4,5,那么后六个默认为0) 35 printf("%d\n",c[5]); 36 //数组名也是数字首地址(数组名的地址和首个数字的地址一样,比喻:一栋5层楼相当于一组数组,1层、2层、3层、4层、5层相当于变量的地址,那么的地址这栋楼的地址与第一层楼的地址一样。) 37 printf("%d\n",c); 38 //指针运算要根据指针的类型(int、float的字节是4个,char的字节是1个,i例如:int类型的a的地址是1,那么a+1的地址是5,如果a类型是char,则a+1的地址是2) 39 printf("%d\n",c+1); 40 // 41 printf("%d\n",*(c+2));//带*的是求地址里面的内容 42 *(c+2) = 0; 43 printf("%d\n",*(c+2)); 44 45 puts("----------------------"); 46 int d = 10; 47 reset(d); 48 //函数独立性 49 printf("%d\n",d); 50 reset2(&d); 51 //使用指针的方式突破函数壁垒 52 printf("%d\n",d); 53 54 //什么是返回值 55 int e = add(3,5); 56 printf("e = %d\n",e); 57 int result = 0; 58 //指针的方式计算结果 59 add_by_point(3,5,&result); 60 printf("result = %d\n",result); 61 62 } 63 void reset(int i){ 64 i = 0; 65 } 66 void reset2(int* p){ 67 *p = 0; 68 } 69 70 71 int add(int i,int j ){ 72 /* 73 * 变量的生命周期 74 * 75 * */ 76 77 int q = i+j; 78 return q; 79 } 80 81 void add_by_point(int x,int y,int *result){ 82 int r = (x + y); 83 *result = r; 84 }


/*
 ============================================================================
 Name        : hello.c
 Author      : lei
 Version     : 137
 Copyright   : Your copyright notice
 Description : Hello World in C, Ansi-style
 ============================================================================
 */

#include <stdio.h>
#include <stdlib.h>

int main(void) {

    main2();
    return 0;
    puts("!!!Hello World!!!"); /* prints !!!Hello World!!! */

    int i = 10;
    reset(i);
    printf("%d\n",i);
    reset2(&i);
    printf("%d\n",i);


    int a = 10;
    int b = &a;
    *(int*)b = 0;


    printf("%d\n",a);

    int *p;
    p=b;//p相当于(int*)b
    *p = 4;
    printf("%d\n",a);

    /*
    int c = 0x22336655;
    int *d = &c;
    *d = 1;
    */

    char aa = 1;
    printf("%d\n",&aa);

    return EXIT_SUCCESS;
}


void reset(int i){
    i = 0;
}

void reset2(int *p){
    *p = 0;
}

 

 

 http://download.csdn.net/album/detail/1111

枚举

 1 /*
 2  * meiju.c
 3  *
 4  *  Created on: 2017年9月15日
 5  *      Author: Administrator
 6  *      137
 7  */
 8 
 9 enum COLOR {
10         RED,YELLOW,BLUE,GREEN
11     };
12 
13 int favorate_color = RED;
14 void main2(){
15 
16     puts("---------------------");
17     //枚举内容默认值从0开始,逐步加1
18     printf("%d\n",RED);
19     printf("%d\n",YELLOW);
20     printf("%d\n",BLUE);
21     puts("---------------------");
22     //枚举内容根据前面的那一个值加1
23     printf("%d\n",RED);
24         printf("%d\n",YELLOW);
25         printf("%d\n",BLUE);
26 
27         set_favorate_color(BLUE);
28 
29         printf("favorate_color = %d\n",favorate_color);
30 
31 }
32 
33 
34 void set_favorate_color(int color){
35     favorate_color = color;
36 
37 }

 机房内容

 1 /*
 2  ============================================================================
 3  Name        : xu.c
 4  Author      : xu
 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 int main(void) {
15     puts("Hello UPC World"); /* prints Hello UPC World */
16     return EXIT_SUCCESS;
17 
18 int a = 0;//定义变量a,赋值为1
19 int *p = &a;//定义一个指针,这个指针的内容是变量a的地址
20 *p = 1;//*P是里面的内容,内容是变量a的地址,把1赋值给P*就是修改a的地址
21 
22 int b = &b;//定义一个b变量,内容是b的地址
23 *(int*)b = 2;//b是变量,(int*)b是把变量b强制转换为地址,*(int*)b是(int*)b地址的内容,也就是改变量b里面的内容
24 
25 puts("----------------------");
26 enum COLOR {
27     red,bule=3,yellow,green
28 };
29 //枚举的特性
30 }
31 
32 void reset1(int i)
33 {
34     i = 0;
35 }
36 void reset2(int *i)
37 {
38     *i = 0;
39 }
40 void reset3(int i)
41 {
42     *(int*)i = 0;
43 
44 }

 

posted @ 2017-09-13 10:26  徐景祥  阅读(505)  评论(0编辑  收藏  举报