Chinaunix首页 | 论坛 | 博客
  • 博客访问: 253018
  • 博文数量: 57
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 102
  • 用 户 组: 普通用户
  • 注册时间: 2015-11-27 15:26
个人简介

自己学习总结,学会总结分享

文章分类

全部博文(57)

文章存档

2016年(49)

2015年(8)

分类: 嵌入式

2016-03-26 01:37:47

指针编程艺术



很容易得出p[-2]=10,p[-1]=20,p[0]=30,p[1]=40,p[2]=50,p[3]=60;

p++,指针p往后移一个单位,则p=a+3,此时p[0]=*(p+0)=40;

*p++, 是先取出*p的值,再执行p++操作, 则*p++=40,p=a+4;

*++p,p先往后移一个单位,再取对应地址的值,则*++p=60;

++*p, 则是先取出*p的值,对*p的值再加1,则++*p=61;


int a=0;
int * const p=&a; //指向整型的常指针 指针指向的内容可变,指针的指向不可变(地址不可变)
const int *p=&a; //指向常整型的指针 指针指向可变,指向内容不可变
const int * const *p=&a;//指向常整型的常指针  指针指向内容和指向都不可变
#include
#include
int main()
{
int a=100, b=200;

const int *p = &a;
int * const q = &b; 
printf("a=%d, *p=%d\n", a, *p);
printf("b=%d, *q=%d\n", b, *q);
/* 不可利用*p 间接更改a变量值 
*p=b;
  */
/* 但可以将p指针指向另一变量的地址 */ 
p=&b;
printf("*p=%d\n", *p);
/*===========================*/

/* 不可更改 q 指针指向另一变量的地址
q=&a;
  */
/* 但可以利用q指针间接更改b变量值 */ 
*q=888;
printf("*q=%d\n", *q);
 
getch();
return 0;
}

数组和指针


1.
    int arr[]= {100, 101, 102};
    int *ptr = arr;
    size = (sizeof arr/ sizeof (arr[0]) );
    &arr[0]  ==  arr+0   ==ptr+0  ==100的地址
    *(arr+0) ==arr[0] ==ptr[0] == *(ptr+0) ==100

     
2.
    int k[3][4] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};

    &k[1][2] ==      *(k + 1) + 2     =k[1]+2     ==6的地址
      上面的表达式加*号为数值6

    *(k[1] + 2)   == *(  (k[1])  +2  )   == k[1][2]  ==6



3
    char *parr[] = {"Department", "of", "Information", "Management"};
    *parr[3] ==  **(parr+3)  ==M
    *(parr+2) == Information


4.
    int arr2[3][4] = {0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22};
    int *parr[3] = {arr2[0], arr2[1], arr2[2]};

    *(parr + 1)  ==   parr[1] + 0 ==8的地址

    *(parr[1] + 1) ==10

    

结构体
   
1. 内存地址

/* 声明结构变量 */

struct employee

{

char id[7]; /* ID号码 */

char name[20]; /* 员工姓名 */

int salary; /* 所得薪资 */

};


/* 定义结构变量,并设定其初始值 */

struct employee manager = {"D54321", "Peter", 35000};


/* 定义结构指针变量ptr,将它指向结构变量manager的地址 */

struct employee *ptr = &manager;


/* 输出内存的地址 */ 

&ptr= 18ff1c

ptr=   18ff20

&manager= 18ff20

manager.id = 18ff20

ptr->id  = D54321

(*ptr).id =D54321



2.结构体定义

struct student {

char firstname[20];

char *lastname;

int score;

};


struct student st1, *st2;


scanf("%s", st1.firstname);  /* 不可以使用指定运算符 */

st1.lastname="小英";       /* 不可以使用scanf函数来输入 */


st2 = (struct student *)malloc(sizeof(struct student));

scanf("%s", st2->firstname);  /* 不可以使用指定运算符 */

st2->lastname="小明";      /* 不可以使用scanf函数来输入 */


3. 结构体传递地址

#include

#include


struct student {

char name[20];

int score;

char *passdown;

};

void passOrdown(struct student *);

void output(struct student *);



int main()

{

struct student stu;

printf("请输入姓名: ");

scanf("%s", stu.name);

printf("请输入C语言的分数: ");

scanf("%d", &stu.score);

 

passOrdown(&stu);

output(&stu);  

getch();

return 0;

}

 

void passOrdown(struct student *p)

{

if (p->score >= 60)

p->passdown = "PASS";  //调用时使用&传递地址,导致函数中改变变量的值可以返回,在output函数中使用得以输出

else 

p->passdown = "DOWN";   

}



void output(struct student *q)

{

int i;

printf("\n\n%10s %10s %20s\n", "Name", "Score", "Pass or Down");  

for(i=1; i<=42; i++)

printf("=");

printf("\n");

printf("%10s %10d %20s\n", q->name, q->score, q->passdown); 

}

结果:













4.结构体数组

struct student stu[3];

  int i;

for(i=0; i<3; i++) {

printf("请输入第 #%d 位同学的姓名: ", i+1);

scanf("%s", stu[i].name);



printf("请输入第 #%d 位同学C语言的分数: ", i+1);

scanf("%d", &stu[i].score);

printf("\n");

}

passOrdown(stu);

output(stu);

getch();

return 0;

}

 

void passOrdown(struct student *p)

{

int i;

for(i=0; i<3; i++) {

if (p->score >= 60)

p->passdown = "PASS";

else 

p->passdown = "DOWN";

p++;

}

}


例子:

struct student st[]= { {"John", 90, st+1}, 

{"Mary", 85, st+2},

{"Peter", 92, st} };



void output(struct student *q)

{

int i;

printf("\n\n%10s %10s %20s\n", "Name", "Score", "Pass or Down");

for(i=1; i<=42; i++)

printf("=");

printf("\n");

for(i=0; i<3; i++) {

printf("%10s %10d %20s\n", q->name, q->score, q->passdown);

q++;

}

}


5.结构体中有结构体

#include
#include
#include
int main()
{
struct node {
char *name;
int score;
struct node *next;
};
struct node *p, *q;
p = (struct node *) malloc(sizeof(struct node));
p->name = "Mary";
p->score = 80;
p->next = NULL;
q = (struct node *) malloc(sizeof(struct node));
q->name = "John";
q->score = 90;
q->next = p;
printf("%s %d\n", q->name, q->score);
printf("%s %d\n", q->next->name, q->next->score);
getch();
return 0;

}

6.


    struct student {

char *name;

int score;

struct student *next;

};

struct student st[]= {  {"John", 90, st+1}, 

{"Mary", 85, st+2},

{"Peter", 92, st} };

struct student *ptr[]={st, st+1, st+2};


++(*ptr)->next)->name =peter

 (*ptr)->next->next->name)=peter



双链表


点击(此处)折叠或打开

  1. /* doublyListRandom.c */
  2. /* 双链表的加入与删除: 以分数的高低加以排序 */


  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <conio.h>


  7. void init_f(void);    /* 初始化链表,建立一空节点为HEAD */
  8. void insert_f(void);    /* 插入函数 */
  9. void delete_f(void);    /* 删除函数 */
  10. void display_f(void);    /* 输出函数 */
  11. void modify_f(void);    /* 修改函数 */
  12.  
  13. struct student {
  14. char name[20];    /* 姓名 */
  15. int score;    /* 分数 */
  16. struct student *llink;    /* 节点左链结 */
  17. struct student *rlink;    /* 节点右链结 */
  18. };


  19. struct student *ptr, *head, *tail, *current_n, *prev;


  20. int main()
  21. {
  22. char option1;


  23. init_f();
  24. while(1){
  25. printf("\n ****************\n");
  26. printf(" 1.insert\n");
  27. printf(" 2.delete\n");
  28. printf(" 3.display\n");
  29. printf(" 4.modify\n");
  30. printf(" 5.quit\n");
  31. printf(" *******************\n");
  32. printf(" Enter your choice (1-5): ");
  33. option1 = getche();
  34. switch(option1){
  35. case '1':
  36. insert_f();
  37. break;
  38. case '2':
  39. delete_f();
  40. break;
  41. case '3':
  42. display_f();
  43. break;
  44. case '4':
  45. modify_f();
  46. break;
  47. case '5':
  48.      printf("\n");
  49. return 0;
  50. }
  51. }
  52. }


  53. void init_f(void) /* 设一HEAD,将左右链结都指向本身 */
  54. {
  55. ptr = (struct student *) malloc(sizeof(struct student));
  56. strcpy(ptr->name, "0");
  57. ptr->llink = ptr;
  58. ptr->rlink = ptr;
  59. head = ptr;
  60. tail = ptr;
  61. }


  62. /* 根据分数的高低加入 */
  63. void insert_f(void)
  64. {
  65. char s_temp[4];


  66. ptr = (struct student *) malloc(sizeof(struct student));
  67. printf("\n Student name : ");
  68. gets(ptr->name);
  69. printf(" Student score: ");
  70. gets(s_temp);
  71. ptr->score = atoi(s_temp);
  72.  
  73. prev = head;
  74. current_n = head->rlink;
  75. while((current_n != head) && (current_n->score >= ptr->score)) {
  76. prev = current_n;
  77. current_n = current_n->rlink;
  78. }
  79. ptr->rlink = current_n;
  80. ptr->llink = prev;
  81. prev->rlink = ptr;
  82. current_n->llink = ptr;
  83. }


  84. void delete_f(void)
  85. {
  86. char del_name[20], ans;
  87. int count = 0;


  88. if(head->rlink == head)
  89. printf("\n No student record\n");
  90. else {
  91. printf("\n Delete student name: ");
  92. gets(del_name);
  93. prev = head;
  94. current_n = head->rlink;
  95. while ((current_n != head) && (strcmp(current_n->name,
  96.                                del_name)!=0)) {
  97. prev = current_n;
  98. current_n = current_n->rlink;
  99. }
  100. if (current_n != head) {
  101. /* 确认是否要删除 */
  102. printf(" Are you sure? ");
  103. ans=getche();
  104. if (ans == 'Y' || ans == 'y') {
  105. prev->rlink = current_n->rlink;
  106. current_n->rlink->llink = prev;
  107. free(current_n);
  108. printf("\n Student %s has been deleted\n",del_name);
  109. }
  110. }
  111. else
  112. printf("\n Student %s not found\n",del_name);
  113. }
  114. }


  115. void modify_f(void)
  116. {
  117. int count = 0;
  118. char n_temp[20], s_temp[4];


  119. if(head->rlink == head)
  120. printf("\n No student recond\n"); /* 无数据显示错误 */
  121. else {
  122. printf("\n Modify student name: ");
  123. gets(n_temp);
  124. current_n=head->rlink;
  125. while ((current_n != head) && (strcmp(current_n->name , n_temp)!=0)){
  126. prev = current_n;
  127. current_n = current_n->rlink;
  128. }
  129. if (current_n != head) {
  130. printf("\n **************************\n");
  131. printf(" Student name : %s\n",current_n->name);
  132. printf(" Student score: %d\n",current_n->score);
  133. printf(" **************************\n");
  134. printf(" Please enter new score: ");
  135. gets(s_temp);
  136. current_n->score = atoi(s_temp);
  137. printf("\n %s student record(s) modified\n", n_temp);
  138. }
  139. else // 找不到数据则显示错误
  140. printf("\n Student %s not found\n",n_temp);
  141. }
  142. }


  143. void display_f(void)
  144. {
  145. int count = 0;


  146. if(head->rlink == head)
  147. printf("\n No student record\n");
  148. else {
  149. printf("\n\n NAME SCORE\n");
  150. printf(" --------------------\n");
  151. current_n = head->rlink;
  152. while(current_n != head) {
  153. printf(" %-10s %3d\n", current_n->name,
  154.        current_n->score);
  155. count++;
  156. current_n = current_n->rlink;
  157. if(count % 20 == 0) getch(); /* 每次输出20条数据 */
  158. }
  159. printf(" ---------------------\n");
  160. printf(" Total %d record(s) found\n", count);
  161. }


队列
                                                                                                                                                                                                                                                         

点击(此处)折叠或打开

  1. /* queue.c */
  2. /* 加入在链表的尾节点和删除在头节点 */
  3. #include <stdio.h>
  4. #include <conio.h>
  5. #include <string.h>
  6. #include <stdlib.h>
  7. #include <ctype.h>
  8. void Insert();
  9. void Delete();
  10. void Display();
  11. void Quit();
  12. struct student
  13. {
  14.     int id;
  15.     int score;
  16.     struct student *next;
  17. };
  18. struct student *x,*head,*current, *prev;

  19. int main()
  20. {
  21.     /* 链表的第一个节点不存放数据 */
  22.     head=(struct student *)malloc(sizeof(struct student));
  23.     head->next= NULL;
  24.     for(;;)
  25.     {
  26.         int a;
  27.         printf("\n");
  28.         printf("1)Insert\n2)Delete\n3)Display\n4)Quit\n");
  29.         printf("Which one: ");
  30.         scanf("%d",&a);
  31.         printf("\n");
  32.         switch(a)
  33.         {
  34.             case 1:Insert();
  35.                     break;
  36.             case 2:Delete();
  37.                     break;
  38.             case 3:Display();
  39.                     break;
  40.             case 4:Quit();
  41.                     break;
  42.         }
  43.     }
  44.     printf("\n\n~~~~~~~~~~~~~~~FINISH~~~~~~~~~~~~~~~~");
  45.     getch();
  46.     return 0;
  47. }

  48. /* 加在尾节点 */
  49. void Insert()
  50. {
  51.     x=(struct student *)malloc(sizeof(struct student));
  52.     printf("Please input your ID: ");
  53.     scanf("%10d",&x->id);
  54.     printf("Please input your score: ");
  55.     scanf("%d",&x->score);
  56.     x->next=NULL;
  57.     
  58.     if(head->next == NULL)
  59.         head->next = x;
  60.     else
  61.     {
  62.         /* 追踪链表的尾节点 */
  63.         current = head->next;
  64.         while(current->next != NULL)
  65.             current = current->next;
  66.         current->next = x;
  67.     }
  68.     printf("====================================\n");
  69. }

  70. /* 删除头节点 */
  71. void Delete()
  72. {
  73.     char ch;

  74.     if(head->next == NULL){
  75.         printf("The queue is empty!\n");
  76.         printf("====================================\n");
  77.         return;
  78.     }
  79.     printf("Are you sure (y/n)? ");
  80.     ch=getche();
  81.     /* 确认是否要删除 */
  82.     if(tolower(ch) == 'y')
  83.     {
  84.         current = head->next;
  85.         head->next = current->next;
  86.         printf("\n%d record has been deleted!!!", current->id);
  87.         free(current);
  88.     }
  89.     printf("\n====================================\n");
  90. }

  91. void Display()
  92. {
  93.     if(head->next == NULL)
  94.         printf("The queue is empty!\n");
  95.     else
  96.     {
  97.         /* 将head 指定给currnt,利用current指针将链表的所有节点输出 */
  98.         current=head->next;
  99.         while(current!=NULL)
  100.         {
  101. printf("ID:%d, score=%d\n", current->id,
  102.                               current->score);
  103.             current=current->next;
  104.         }
  105.     }
  106.     printf("====================================\n");
  107. }

  108. void Quit()
  109. {
  110.     exit(0);
  111. }


            

点击(此处)折叠或打开

  1. /* stack.c */
  2. /* 加入和删除都在链表的头节点 */
  3. #include <stdio.h>
  4. #include <conio.h>
  5. #include <string.h>
  6. #include <stdlib.h>
  7. #include <ctype.h>
  8. void Insert();
  9. void Delete();
  10. void Display();
  11. void Quit();
  12. struct student
  13. {
  14.     int id;
  15.     int score;
  16.     struct student *next;
  17. };
  18. struct student *x,*head,*current;

  19. int main()
  20. {
  21.     /* 链表的第一个节点不存放数据 */
  22.     head=(struct student *)malloc(sizeof(struct student));
  23.     head->next= NULL;
  24.     for(;;) {
  25.     int a;
  26.     printf("\n");
  27.     printf("1)Insert\n2)Delete\n3)Display\n4)Quit\n");
  28.     printf("Which one: ");
  29.     scanf("%d",&a);
  30.     printf("\n");
  31.     switch(a)
  32.     {
  33.     case 1:Insert();
  34.     break;
  35.     case 2:Delete();
  36.     break;
  37.     case 3:Display();
  38.     break;
  39.     case 4:Quit();
  40.     break;
  41.     }
  42.     }
  43.     printf("\n\n~~~~~~~~~~~~~~~FINISH~~~~~~~~~~~~~~~~");
  44.     getch();
  45.     return 0;
  46. }
  47.  
  48. /* 加入一节点于链表头节点 */
  49. void Insert()
  50. {
  51.     /* 分配内存,并指定数据 */
  52.     x=(struct student *)malloc(sizeof(struct student));
  53.     printf("Please input your ID: ");
  54.     scanf("%d",&x->id);
  55.     printf("Please input your score: ");
  56.     scanf("%d",&x->score);
  57.     x->next=NULL;
  58.     
  59.     x->next=head->next;
  60.     head->next=x;
  61.     printf("====================================\n");
  62. }

  63. /* 删除链表头节点的节点 */
  64. void Delete()
  65. {
  66.     char ch;
  67.     /* 判断链表是否有数据 */
  68.     if(head->next == NULL)
  69.     {
  70.     printf("The stack is empty!\n");
  71.     printf("====================================\n");
  72.     return;
  73.     }
  74.     printf("Are you sure (y/n)? ");
  75.     ch=getche();
  76.     
  77.     if(tolower(ch) == 'y')
  78.       {
  79.     current=head->next;
  80.     printf("\n%d record has been deleted!!!", current->id);
  81.     head->next=current->next;
  82.     free(current);
  83.     }
  84.     printf("\n====================================\n");
  85. }

  86. void Display()
  87. {
  88.     if(head->next == NULL)
  89.     printf("The stack is empty!\n");
  90.     else
  91.     {
  92.     current=head->next;
  93.     while(current!=NULL)
  94.     {
  95. printf("ID:%d, score=%d\n",current->id,current->
  96.         score);
  97. current=current->next;
  98.     }
  99.     }
  100.     printf("====================================\n");
  101. }

  102. void Quit()
  103. {
  104.     exit(0);
  105. }


链表

        

点击(此处)折叠或打开

  1. /* singleListRandom.c */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <conio.h>
  6.  
  7. void insert_func(void);
  8. void sort_func(void);
  9. void delete_func(void);
  10. void display_func(void);
  11. void modify_func(void);
  12. void anykey_func(void);

  13. struct student {
  14.     char name[20];
  15.     int score;
  16.     struct student *next;
  17. };
  18. struct student *ptr, *head, *current, *prev;

  19. int main()
  20. {
  21.     char option1;
  22.     head = (struct student *) malloc(sizeof(struct student));
  23.     head->next=NULL;
  24.     while(1) {
  25.         printf("\n");
  26.         printf("******************\n");
  27.         printf(" 1.insert\n");
  28.         printf(" 2.delete\n");
  29.         printf(" 3.display\n");
  30.         printf(" 4.modify\n");
  31.         printf(" 5.quit\n");
  32.         printf("*******************\n");
  33.         printf(" Enter your choice (1-5): ");
  34.         option1=getche();
  35.         printf("\n");
  36.         switch(option1)    {
  37.             case '1':
  38.                 insert_func();
  39.                 break;
  40.             case '2':
  41.                 delete_func();
  42.                 break;
  43.             case '3':
  44.                 display_func();
  45.                 break;
  46.             case '4':
  47.                 modify_func();
  48.                 break;
  49.             case '5':
  50.                  printf("\n");
  51.                 return 0;
  52.         }
  53.     }
  54. }

  55. /* 加入是以分数的高低加以排序 */
  56. void insert_func(void)
  57. {
  58.     char s_temp[4];
  59.     ptr=(struct student *) malloc(sizeof(struct student));
  60.     printf(" Student name : ");
  61.     gets(ptr->name);
  62.     printf(" Student score: ");
  63.     gets(s_temp);
  64.     ptr->score = atoi(s_temp);
  65.  
  66.     prev = head;
  67.     current = head->next;
  68.     while ((current != NULL) && (current->score > ptr->score)) {
  69.         prev = current;
  70.         current = current->next;
  71.     }
  72.     ptr->next = current;
  73.     prev->next = ptr;
  74. }

  75. /* 删除是以姓名为键值*/
  76. void delete_func(void)
  77. {
  78.     char del_name[20], ans;
  79.     printf(" Delete student name: ");
  80.     gets(del_name);
  81.     
  82.     prev = head;
  83.     current = head->next;
  84. while ((current != NULL) && (strcmp(current->name ,
  85.         del_name)!=0)) {
  86.         prev = current;
  87.         current = current->next;
  88.     }
  89.     if (current != NULL) {
  90.         /* 确认是否要删除 */
  91.         printf(" Are you sure? ");
  92.         ans=getche();
  93.         if (ans == 'Y' || ans == 'y') {
  94.             prev->next = current->next;
  95.             free(current);
  96.             printf(" Student %s has been deleted\n", del_name);
  97.         }
  98.     }
  99.     else
  100.         printf(" Student %s not found\n",del_name);
  101.  }

  102. void modify_func(void)
  103. {
  104.     char n_temp[20],s_temp[4];
  105.     printf(" Modify student name: ");
  106.     gets(n_temp);
  107.     current=head->next;

  108. while ((current != NULL) && (strcmp(current->name , n_temp)!=0)) {        
  109. prev = current;
  110.      current = current->next;
  111.     }
  112.     if (current != NULL) {
  113.         printf(" **************************\n");
  114.         printf(" Student name : %s\n",current->name);
  115.         printf(" Student score: %d\n",current->score);
  116.         printf(" **************************\n");
  117.         printf(" Please enter new score: ");
  118.         gets(s_temp);
  119.         current->score = atoi(s_temp);
  120.         printf(" Student %s has been modified\n",n_temp);
  121.     }
  122.     else
  123.         printf(" Student %s not found\n",n_temp);
  124. }

  125. void display_func(void)
  126. {
  127.     int count=0;

  128.     if(head->next == NULL) {
  129.         printf(" No student record\n");
  130.     }
  131.     else {
  132.         printf("\n");
  133.         printf(" NAME SCORE\n");
  134.         printf(" ----------------\n");
  135.         current=head->next;
  136.         while(current != NULL) {
  137. printf(" %-10s %3d\n", current->name,
  138.         current->score);
  139.             count++;
  140.             current=current->next;
  141.             if(count % 20 == 0) getch();
  142.         }
  143.         printf(" ----------------\n");
  144.         printf(" Total %d record(s) found\n", count);
  145.     }
  146. }




阅读(1874) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~