|
|
今日学习任务
|
嵌入式 C 语言开发实战 |
今日任务完成情况
| 今日任务按计划完成 1.掌握函数指针(void (*a[4])())和函数指针数组(int (*p_cal_array[4])(int , int))以及回调函数(int cal (int a,int b,int(*p_cal)(int , int))) 2.熟练掌握所有关键字的使用;(register、static、extern、const、typedef) 3.熟练掌握复合数据类型的使用;(struct、union、enum) 4.掌握宏定义的作用和宏函数 5.熟练掌握循环语句(for、while、switch、goto等)(break、continue等) 6.编程技巧总结; |
今日开发中出现的问题汇总
|
|
今日未解决问题
| 无 |
今日开发收获 | 1.掌握函数指针(void (*a[4])())和函数指针数组(int (*p_cal_array[4])(int , int))以及了解和体验了回调函数的使用(int cal (int a,int b,int(*p_cal)(int , int))) 2.熟练掌握所有关键字的使用;(register:寄存器变量、static(只执行一次)、extern、const(就近原则)、typedef(提高代码的可移植性和可读性)) 3.熟练掌握复合数据类型的使用;(struct(封装数据、打包数据)、union(字节长度:占用字节最多的成员的字节的倍数,而且需要能够容纳其他的成员;缺点:造成数据覆盖;大端字节序和小端字节序;以及用共同体判断是大端还是小端字节序的方法)、enum(里面放的是一系列整数宏);字节长度是4个字节) 4.掌握宏定义的作用(提高代码的可读性、移植性,解决幻数问题)和宏函数(#define MAX(a,b) a>b?a:b) 5.熟练掌握循环语句(for、while、switch、goto(解决异常处理)等)(break:跳出整个程序、continue:结束这一次循环,回到判断语句等)
|
自我评价
| 今天能够跟上老师的教学安排 |
其他
| 无 |
1.回调函数的简单使用
1 #include <stdio.h>
2
3 int add(int a, int b)
4 {
5 return a + b;
6 }
7
8 int sub(int a, int b)
9 {
10 return a - b;
11 }
12
13 int mul(int a, int b)
14 {
15 return a * b;
16 }
17
18 int div(int a, int b)
19 {
20 return a / b;
21 }
22
23 int cal(int a, int b, int (*p_cal)(int ,int)) //回调函数
24 {
25 return p_cal(a,b);
26 }
27
28 int main()
29 {
30 int i;
31 int sum;
32 cal(6,6,add);
33 cal(6,6,sub);
34 cal(6,6,mul);
35 cal(6,6,div);
36
37
38 int (*p_cal_array[4])(int, int);
39
40 p_cal_array[0] = add;
41 p_cal_array[1] = sub;
42 p_cal_array[2] = mul;
43 p_cal_array[3] = div;
44
45
46 for(i = 0; i < 4; i++)
47 {
48 sum = (p_cal_array[i])(6,6);
49 printf("sum = %d\n",sum);
50 }
51
52 //sum = (*p_cal_array[1])(6,6);
53 //printf("sum = %d",sum);
54 //printf("\n");
55 return 0;
56 }
57
2.结构体的用法
1 #include <stdio.h>
2
3 struct massage
4 {
5 int num;
6 char ch;
7 char *ptr;
8 char str[100];
9 };
10
11 typedef struct massage Massage;
12
13 int main()
14 {
15 int num = 6;
16 char ch = 'a';
17
18 printf("num = %d, ch = %c\n",num,ch);
19
20 Massage meg;
21 Massage *p = &meg;
22
23 meg.num = 1;
24 meg.ch = 'a';
25 p->num = 6;
26 p->ch = 'c';
27
28 strcpy(p->str,"hello world");
29
30 printf("p->num = %d, p->ch = %c \n",p->num,p->ch);
31
32
33
34 return 0;
35 }
3.测量结构体的字节长度
1 #include <stdio.h>
2
3 struct node
4 {
5 char i;
6 int num;
7 double j;
8
9 };
10
11 typedef struct node Node;
12
13 int main()
14 {
15 Node p;
16 printf("sizeof(p) = %d\n",sizeof(p));
17 return 0;
18 }
4.共同体的用法
1 #include <stdio.h>
2
3 union node
4 {
5 int num;
6 char ch[2];
7 };
8
9 int main()
10 {
11 union node p;
12 union node *pp = &p;
13
14 p.num = 1;
15 pp->num = 1;
16
17 p.ch[0] = 'b';
18 p.ch[1] = 'a';
19 pp->ch[0] = 'b';
20 pp->ch[1] = 'a';
21 printf("pp->num=%d,pp->ch[0]=%c,pp->ch[1]=%c\n",
pp->num,pp->ch[0],pp->ch[1]);
22
23 return 0;
24 }
5.用共同体判断是大端字节序还是小端字节序
1 #include <stdio.h>
2 union node
3 {
4 int num;
5 char ch;
6 };
7
8 int main()
9 {
10 union node p;
11 p.num = 0x12345678;
12 if(p.ch == 0x78)
13 {
14 printf("little\n");
15 }
16 if(p.ch == 0x12)
17 {
18 printf("big\n");
19 }
20 return 0;
21 }