1.变量的存放位置

注意:volatile
volatile是一个特征修饰符(type specifier).volatile的作用是作为指令关键字,确保本条指令不会因编译器的优化而省略,且要求每次直接读值。
2.结构体的存在空间
#include<stdio.h>#include<string.h>#include <stdlib.h>struct person {char* name;int age;};struct person2 {char name[100];int age;};struct person3 {char sex;int age;};struct person4 {char sex;char high;int age;};struct company {char* name;struct person worker[100];};int main(void){struct person wei = { "weidongshan", 40 };printf("sizeof(int) = %d, sizeof(char) = %d\r\n", sizeof(int), sizeof(char));printf("sizeof(int *) = %d, sizeof(char *) = %d\r\n", sizeof(int*), sizeof(char*));printf("sizeof(struct person) = %d, sizeof(struct person2) = %d\r\n", \sizeof(struct person), sizeof(struct person2));printf("sizeof(struct person *) = %d, sizeof(struct person2 *) = %d\r\n", \sizeof(struct person*), sizeof(struct person2*));printf("sizeof(struct person3) = %d, sizeof(struct person3 *) = %d\r\n", \sizeof(struct person3), sizeof(struct person3*));printf("sizeof(struct person4) = %d, sizeof(struct person4 *) = %d\r\n", \sizeof(struct person4), sizeof(struct person4*));printf("name = %s, age = %d\r\n", wei.name, wei.age);while (1);return 0;}
3.结构体的存在空间
#include<stdio.h>#include<string.h>#include <stdlib.h>typedef struct student {char* name;int age;struct student* classmate;}student, * pstudent;int main(void){student zhangsan = { "zhangshan", 10, NULL };student lili = { "lili", 10, NULL };zhangsan.classmate = &lili;lili.classmate = &zhangsan;printf("zhangsan's classmate is :%s\r\n", zhangsan.classmate->classmate->classmate->name);while (1);return 0;}
4.函数指针
#include<stdio.h>#include<string.h>#include <stdlib.h>typedef struct student {char* name;int age;void (*good_work)(void);struct student* classmate;}student, * pstudent;static void play_ball(void){printf("playing ball\r\n");}static void sing_song(void){printf("singing\r\n");}int main(void){int i;//函数有特权可以不加取址符号student ss[2] = { {"zhangshan", 10, &play_ball, NULL}, {"lili", 10, sing_song, NULL} };for (i = 0; i < 2; i++){ss[i].good_work();}while (1);return 0;}
5.函数指针的使用
#include<stdio.h>#include<string.h>#include <stdlib.h>typedef struct lcd_operation {int type;void (*draw_logo)(void);}lcd_operation, * p_lcd_operation;int read_eeprom(){/* 0: lcd a* 1: lcd b*/return 0;}int get_lcd_type(void){return read_eeprom();}void draw_logo_lcda(void){printf("display logo on lcd a\r\n");}void draw_logo_lcdb(void){printf("display logo on lcd b\r\n");}lcd_operation xxx_com_lcds[2] = {{0, draw_logo_lcda},{1, draw_logo_lcdb},};p_lcd_operation get_lcd(void){int type = get_lcd_type();return &xxx_com_lcds[type];}//结构体成员的初始化lcd_operation lcd_a_ops = {.type = 0,.draw_logo = draw_logo_lcda,};//add_type是个函数指针,返回值为int,参数为(int,int)typedef int (*add_type)(int, int);add_type f1;add_type f2;int (*f3)(int, int);int (*f4)(int, int);int main(void){p_lcd_operation lcd;lcd = get_lcd();lcd->draw_logo();while (1);return 0;}
6.单向链表的使用
#include<stdio.h>#include<string.h>#include <stdlib.h>typedef struct spy {char* name;struct spy* next;}spy, * p_spy;spy A = { "A", NULL };spy B = { "B", NULL };spy C = { "C", NULL };spy D = { "D", NULL };spy E = { "E", NULL };p_spy head = NULL;//链表的尾插法void insert_spy(p_spy newspy){p_spy last;if (head == NULL){head = newspy;newspy->next = NULL;}else{/* 先找到链表的最后一项 last */last = head;while (last){if (last->next == NULL) /* 找到了 */break;elselast = last->next;}/* */last->next = newspy;newspy->next = NULL;}}//链表的删除void remove_spy(p_spy oldspy){p_spy left;if (head == oldspy){head = oldspy->next;}else{/* 找出oldspy的上线 */left = head;while (left){if (left->next == oldspy)break;elseleft = left->next;}if (left){left->next = oldspy->next;}}}void print_spy(void){p_spy tmp = head;while (tmp){printf("%s\r\n", tmp->name);tmp = tmp->next;}}int main(void){/*A.next = &B;B.next = &C;C.next = NULL;*/insert_spy(&A);insert_spy(&B);insert_spy(&C);insert_spy(&D);head = &A;print_spy();remove_spy(&D);printf("remove spy B: \r\n");print_spy();system("pause");return 0;}

