c疑难点

Posted 飞雪天龙

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c疑难点相关的知识,希望对你有一定的参考价值。



很容易得出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 <stdio.h>

#include <conio.h>


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 <stdio.h>
#include <stdlib.h>
#include <conio.h>
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. char option1;


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





  49. void init_f(void) /* 设一HEAD,将左右链结都指向本身 */

  50. ptr = (struct student *) malloc(sizeof(struct student));
  51. strcpy(ptr->name, "0");
  52. ptr->llink = ptr;
  53. ptr->rlink = ptr;
  54. head = ptr;
  55. tail = ptr;



  56. /* 根据分数的高低加入 */
  57. void insert_f(void)

  58. char s_temp[4];


  59. ptr = (struct student *) malloc(sizeof(struct student));
  60. printf("\\n 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_n = head->rlink;
  68. while((current_n != head) && (current_n->score >= ptr->score))
  69. prev = current_n;
  70. current_n = current_n->rlink;

  71. ptr->rlink = current_n;
  72. ptr->llink = prev;
  73. prev->rlink = ptr;
  74. current_n->llink = ptr;



  75. void delete_f(void)

  76. char del_name[20], ans;
  77. int count = 0;


  78. if(head->rlink == head)
  79. printf("\\n No student record\\n");
  80. else
  81. printf("\\n Delete student name: ");
  82. gets(del_name);
  83. prev = head;
  84. current_n = head->rlink;
  85. while ((current_n != head) && (strcmp(current_n->name,
  86.                                del_name)!=0))
  87. prev = current_n;
  88. current_n = current_n->rlink;

  89. if (current_n != head)
  90. /* 确认是否要删除 */
  91. printf(" Are you sure? ");
  92. ans=getche();
  93. if (ans == 'Y' || ans == 'y')
  94. prev->rlink = current_n->rlink;
  95. current_n->rlink->llink = prev;
  96. free(current_n);
  97. printf("\\n Student %s has been deleted\\n",del_name);


  98. else
  99. printf("\\n Student %s not found\\n",del_name);




  100. void modify_f(void)

  101. int count = 0;
  102. char n_temp[20], s_temp[4];


  103. if(head->rlink == head)
  104. printf("\\n No student recond\\n"); /* 无数据显示错误 */
  105. else
  106. printf("\\n Modify student name: ");
  107. gets(n_temp);
  108. current_n=head->rlink;
  109. while ((current_n != head) && (strcmp(current_n->name , n_temp)!=0))
  110. prev = current_n;
  111. current_n = current_n->rlink;

  112. if (current_n != head)
  113. printf("\\n **************************\\n");
  114. printf(" Student name : %s\\n",current_n->name);
  115. printf(" Student score: %d\\n",current_n->score);
  116. printf(" **************************\\n");
  117. printf(" Please enter new score: ");
  118. gets(s_temp);
  119. current_n->score = atoi(s_temp);
  120. printf("\\n %s student record(s) modified\\n", n_temp);

  121. else // 找不到数据则显示错误
  122. printf("\\n Student %s not found\\n",n_temp);




  123. void display_f(void)

  124. int count = 0;


  125. if(head->rlink == head)
  126. printf("\\n No student record\\n");
  127. else
  128. printf("\\n\\n NAME SCORE\\n");
  129. printf(" --------------------\\n");
  130. current_n = head->rlink;
  131. while(current_n != head)
  132. printf(" %-10s %3d\\n", current_n->name,
  133.        current_n->score);
  134. count++;
  135. current_n = current_n->rlink;
  136. if(count % 20 == 0) getch(); /* 每次输出20条数据 */

  137. printf(" ---------------------\\n");
  138. printf(" Total %d record(s) found\\n", count);


队列
                                                                                                                                                                                                                                                         

点击(此处)折叠或打开

  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.     int id;
  14.     int score;
  15.     struct student *next;
  16. ;
  17. struct student *x,*head,*current, *prev;

  18. int main()

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


  45. /* 加在尾节点 */
  46. void Insert()

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


  65. /* 删除头节点 */
  66. void Delete()

  67.     char ch;

  68.     if(head->next == NULL)
  69.         printf("The queue is empty!\\n");
  70.         printf("====================================\\n");
  71.         return;
  72.     
  73.     printf("Are you sure (y/n)? ");
  74.     ch=getche();
  75.     /* 确认是否要删除 */
  76.     if(tolower(ch) == 'y')
  77.     
  78.         current = head->next;
  79.         head->c疑难点

    第四周疑难点

    Java SE 疑难点记录

    2018年最热门的JavaScript开源项目!

    TCP协议疑难杂症全景解析|硬核

    Java集合 -- 疑难点总结(Arrays.asList()正确使用Collection.toArray()正确使用反转数组 foreach 循环不要进行元素的 remove/add 操作)