鱼C论坛

 找回密码
 立即注册
查看: 1828|回复: 16

[已解决]大佬们看一下为毛addstu函数里的ptr指针的地址是0x0

[复制链接]
发表于 2019-3-28 16:31:53 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>

  4. struct students{
  5.         char name[10];
  6.         int num;
  7.         float score;
  8.         struct students *next;       
  9. };


  10. int count=0;
  11. int *p = &count;

  12. void getInfo(struct students *);
  13. int addStu(struct students **);
  14. int delStu(struct students **,int );
  15. int searchStu(struct students *,int );
  16. void showList(struct students **);

  17. int main(){
  18.         struct students *S = NULL;//生成一个头节点
  19.         int op,i;
  20.         do{
  21.         printf("welcome to students' information manage system!\n");
  22.         printf("please chose your operation:\n");
  23.         printf("1 add a student to specified location\n");
  24.         printf("2 delete a student from the specified location\n");
  25.         printf("3 find a student through his number\n");
  26.         printf("4 show the list:\n");
  27.         printf("0 exit\n");
  28.         scanf("%d",&op);
  29.                 switch(op){
  30.                         case 1:printf("input the num you want to insert:");addStu(&S);break;
  31.                         case 2:printf("input the num you want to delete:");scanf("%d",&i);delStu(&S,i);break;
  32.                         case 3:printf("input his num:");scanf("%d",&i);searchStu(S,i);break;
  33.                         case 4:showList(&S);break;
  34.                 }
  35.         }while(op != 0);
  36.        
  37.         return 0;
  38. }
  39. void getInfo(struct students *s){
  40.         printf("input students's name :\n");
  41.         scanf("%s",s->name);
  42.         printf("input students's num and score:\n");
  43.         scanf("%d%f",&s->num,&s->score);
  44. }
  45. int addStu(struct students **S1){ //问题出在这个函数里
  46.         struct students *ns,*temp,*ptr = *S1;
  47.         ns = (struct students *)malloc(sizeof(struct students));
  48.         if(ns == NULL){
  49.                 printf("menory allocation error!");
  50.                 exit(1);
  51.         }
  52.         getInfo(ns);
  53.         if(ptr == NULL){
  54.                 *S1 = ns;
  55.                 ns->next = NULL;
  56.                 (*p)++;
  57.         }
  58.         while(ptr->next != NULL && ptr->num < ns->num){
  59.                 temp = ptr;
  60.                 ptr = ptr->next;
  61.         }
  62.         if(ptr == *S1){
  63.                 temp = *S1;
  64.                 *S1 = ns;
  65.                 ns->next = temp;
  66.         }
  67.         else if(ptr->next == NULL){
  68.                 ptr->next = ns;
  69.                 ns->next = NULL;
  70.                
  71.         }
  72.         else{
  73.                 temp->next = ns;
  74.                 ns->next = ptr;
  75.         }
  76.         printf("name\tnum\tscore\t\n",(*S1)->name,(*S1)->num,(*S1)->score);
  77.         return 0;
  78. }
  79. int delStu(struct students **S1,int n){
  80.         struct students *temp,*ptr = *S1; //用来接收旧的结构体节点,并准备释放内存
  81.         if(ptr->next == NULL){
  82.                 printf("can't delete student from an empty list\n");
  83.         }
  84.         while(ptr->num < n){
  85.                 ptr=ptr->next;
  86.         }
  87.         if(ptr == NULL){
  88.                 printf("can't find this student!\n");
  89.         }
  90.         if(ptr->num == n){
  91.                 temp = ptr;
  92.                 ptr = temp->next;
  93.                 free(temp);
  94.                 printf("delete complete\n");
  95.         }
  96.         return 0;
  97. }
  98. int searchStu(struct students *S1,int n){
  99.         int i;
  100.         while(S1 != NULL){
  101.                 if(S1->num == n){
  102.                         printf("he/she is the %d student");
  103.                         return i+1;
  104.                 }
  105.                 S1 = S1->next;
  106.         }
  107.         if(S1->next == NULL && S1->num != n){
  108.                         printf("can't find this student");
  109.                         return 0;
  110.                 }
  111. }
  112. void showList(struct students **S1){
  113.         if(*S1 == NULL){
  114.                 printf("no student in this list!\n");
  115.         }
  116.         else{
  117.                 printf("name\tnum\tscore\t\n");
  118.         }
  119.         while(*S1 != NULL){
  120.                 printf("%s\t%d\t%d\t\n",(*S1)->name,(*S1)->num,(*S1)->score);
  121.                 *S1 = (*S1)->next;
  122.         }
  123.        
  124. }
复制代码
最佳答案
2019-3-31 14:27:19
本帖最后由 Croper 于 2019-3-31 22:49 编辑

知道你的意思,你是想move改变的时候(*S1)同时改变是么,


C语言里,仍然只能使用二重指针
  1. struct student **move=S1;
复制代码

然后使用(*move)代替你原代码里的move

//=================这里是分析环节==================
你前面这一段程序,相当于
  1. int main(){
  2.         int S=0;
  3.         int *S1=&S;
  4.         int move=*S1;
  5. }
复制代码

S1指向S,你认为,改变move的值能够改变S的值么,
他们分别属于两个不同的内存,这当然是不可能的

就算加上你的常指针
  1. int main(){
  2.         int S=0;
  3.         int *S1=&S;
  4.         int move=*(int* const )S1;
  5. }
复制代码

你认为这有区别么


唯一的方法只能
  1. int main(){
  2.         int S=0;
  3.         int *S1=&S;
  4.         int *move=S1;
  5. }
复制代码

这样才能做到改变(*move)就是改变S
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-3-28 16:32:46 | 显示全部楼层
第51行,ptr已经赋值了,但是却不是*S1的地址
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-3-28 16:41:15 | 显示全部楼层
本帖最后由 Croper 于 2019-3-28 16:50 编辑
  1. if(ptr == NULL){
  2.                 *S1 = ns;   //这里,*S1指向的地址已经被改变了,但ptr仍然指向*S1原来的地址,也就是nullptr
  3.                 ns->next = NULL;
  4.                 (*p)++;
  5.         }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-3-30 14:36:05 | 显示全部楼层

请问大佬这个是不是可以用常指针来解决问题
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-3-30 14:42:39 | 显示全部楼层
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>

  4. struct students{
  5.         char name[10];
  6.         int num;
  7.         float score;
  8.         struct students *next;       
  9. };


  10. int count=0;
  11. int *p = &count;

  12. void getInfo(struct students *);
  13. void addStu(struct students **);
  14. int delStu(struct students **,int );
  15. int searchStu(struct students *,int );
  16. void showList(struct students **);

  17. int main(){
  18.         struct students *S = NULL;//生成一个头节点
  19.         int op,i;
  20.         do{
  21.         printf("welcome to students' information manage system!\n");
  22.         printf("please chose your operation:\n");
  23.         printf("1 add a student to specified location\n");
  24.         printf("2 delete a student from the specified location\n");
  25.         printf("3 find a student through his number\n");
  26.         printf("4 show the list:\n");
  27.         printf("0 exit\n");
  28.         scanf("%d",&op);
  29.                 switch(op){
  30.                         case 1:printf("input the student information:");addStu(&S);break;
  31.                         case 2:printf("input the num you want to delete:");scanf("%d",&i);delStu(&S,i);break;
  32.                         case 3:printf("input his num:");scanf("%d",&i);searchStu(S,i);break;
  33.                         case 4:showList(&S);break;
  34.                 }
  35.         }while(op != 0);
  36.        
  37.         return 0;
  38. }
  39. void getInfo(struct students *s){
  40.         printf("input students's name :\n");
  41.         scanf("%s",s->name);
  42.         printf("input students's num and score:\n");
  43.         scanf("%d%f",&s->num,&s->score);
  44. }
  45. void addStu(struct students **S1){
  46.         struct students *ns,*temp,*move = (struct students * const)S1;
  47.         ns = (struct students *)malloc(sizeof(struct students));
  48.         if(ns == NULL){
  49.                 printf("menory allocation error!");
  50.                 exit(1);
  51.         }
  52.         getInfo(ns);
  53.         if(*S1 == NULL){ //空链表的操作;
  54.                 move=ns;
  55.                 ns->next=NULL;
  56. //                move = move->next;
  57.         }
  58.         else{
  59.                 while(move->num<ns->num && move->next != NULL){//通过新学生的学号来移动move与temp指针,
  60.                         temp = move;
  61.                         move = move->next;
  62.                 }
  63.                 if(move == *S1){ //如果新来的学生的学号是最小的,那么move指针并不会移动,所以move指针指向头节点
  64.                         ns->next = move->next;
  65.                         move = ns;
  66.                 }
  67.                 else if(move->next == NULL && move->num<ns->num){ //新学生的学号是最大的
  68.                         move->next = ns;
  69.                         ns->next = NULL;
  70.                 }
  71.                 else{ //学生学号刚好在中间
  72.                         temp->next = ns;
  73.                         ns->next = move;       
  74.                 }
  75.        
  76.         }
  77.         printf("%s\t%d\t%d\t\n",move->name,move->num,move->score);
  78. //        printf("%s\t%d\t%d\t\n",(*S1)->name,(*S1)->num,(*S1)->score);
  79. }
  80. int delStu(struct students **S1,int n){
  81.         struct students *temp,*ptr = *S1; //用来接收旧的结构体节点,并准备释放内存
  82.         if(ptr->next == NULL){
  83.                 printf("can't delete student from an empty list\n");
  84.         }
  85.         while(ptr->num < n){
  86.                 ptr=ptr->next;
  87.         }
  88.         if(ptr == NULL){
  89.                 printf("can't find this student!\n");
  90.         }
  91.         if(ptr->num == n){
  92.                 temp = ptr;
  93.                 ptr = temp->next;
  94.                 free(temp);
  95.                 printf("delete complete\n");
  96.         }
  97.         return 0;
  98. }
  99. int searchStu(struct students *S1,int n){
  100.         int i = 0;
  101.         while(S1 != NULL){
  102.                 if(S1->num == n){
  103.                         printf("he/she is the %d student",i);
  104.                         return i+1;
  105.                 }
  106.                 S1 = S1->next;
  107.                 i++;
  108.         }
  109.         if(S1->next == NULL && S1->num != n){
  110.                         printf("can't find this student");
  111.                         return 0;
  112.                 }
  113.         return 0;
  114. }
  115. void showList(struct students **S1){
  116.         struct students *s;
  117.         s = *S1;
  118.         if(*S1 == NULL){
  119.                 printf("no student in this list!\n");
  120.         }
  121.         else{
  122.                 printf("name\tnum\tscore\t\n");
  123.         }
  124.         while(s != NULL){
  125.                 printf("%s\t%d\t%d\t\n",s->name,s->num,s->score);
  126.                 s = s->next;
  127.         }
  128.        
  129. }
复制代码

使用常指针的代码,但是不知道为什么,*S1就是一直是空的,求大佬们看看思路哪里有错吧
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-3-30 14:50:51 | 显示全部楼层
cookies945 发表于 2019-3-30 14:36
请问大佬这个是不是可以用常指针来解决问题


所以我推荐链表在初始化时就申请一个空置的头节点,然后把这个头节点作为唯一参照物,
然后所有过程都不需要讨论头结点为空的情况了

你这个ptr再重新指定一下就好啊
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-3-31 10:59:05 | 显示全部楼层
Croper 发表于 2019-3-30 14:50
所以我推荐链表在初始化时就申请一个空置的头节点,然后把这个头节点作为唯一参照物,
然后所有过程都不 ...

还是没有怎么听懂,我在main函数中初始化的头指针就是空,然后再将其传入addStu的函数的时候把它作为常指针了,是哪里思路有问题么,还是我们讨论的不是同一个指针
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-3-31 14:27:19 | 显示全部楼层    本楼为最佳答案   
本帖最后由 Croper 于 2019-3-31 22:49 编辑

知道你的意思,你是想move改变的时候(*S1)同时改变是么,


C语言里,仍然只能使用二重指针
  1. struct student **move=S1;
复制代码

然后使用(*move)代替你原代码里的move

//=================这里是分析环节==================
你前面这一段程序,相当于
  1. int main(){
  2.         int S=0;
  3.         int *S1=&S;
  4.         int move=*S1;
  5. }
复制代码

S1指向S,你认为,改变move的值能够改变S的值么,
他们分别属于两个不同的内存,这当然是不可能的

就算加上你的常指针
  1. int main(){
  2.         int S=0;
  3.         int *S1=&S;
  4.         int move=*(int* const )S1;
  5. }
复制代码

你认为这有区别么


唯一的方法只能
  1. int main(){
  2.         int S=0;
  3.         int *S1=&S;
  4.         int *move=S1;
  5. }
复制代码

这样才能做到改变(*move)就是改变S
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-3-31 16:23:16 | 显示全部楼层
本帖最后由 Croper 于 2019-3-31 16:38 编辑

我按你的风格重写了一个,自己对照着改改吧
  1. #include <stdio.h>

  2. struct students{
  3.         char name[10];
  4.         int num;
  5.         float score;
  6.         struct students *next;        
  7. };

  8. struct students* CreateStudent();
  9. void addStu(struct students *,struct students *);
  10. int delStu(struct students *,int );
  11. struct students* searchStu(struct students *,int );
  12. void showList(struct students *);
  13. void PrintStudentInfo(struct students *);

  14. struct students* prehead;

  15. void flushstdin(){
  16.         char c;
  17.         while ((c=getchar())!=EOF && c!='\n');
  18. }

  19. void PrintStartInterface(){
  20.             printf("**********************************************************************\n");
  21.         printf("*        welcome to students' information manage system!             *\n");
  22.         printf("*          please chose your operation:                              *\n");
  23.         printf("*                                                                    *\n");
  24.         printf("*          1 add a student to specified location                     *\n");
  25.         printf("*          2 delete a student from the specified location            *\n");
  26.         printf("*          3 find a student through his number                       *\n");
  27.         printf("*          4 show the list:                                          *\n");
  28.         printf("*          0 exit                                                    *\n");
  29.         printf("**********************************************************************\n\n");
  30. }

  31. void InitAll(){
  32.         prehead=(struct students*)malloc(sizeof(struct students));
  33.         prehead->next=NULL;
  34. }

  35. int PostOperation(){
  36.         int op;
  37.         scanf("%c",&op);
  38.         flushstdin();
  39.        
  40.         switch (op){
  41.                 case '1':{
  42.                         struct students *p=CreateStudent();
  43.                         addStu(prehead,p);
  44.                         printf("Add student success!\n");
  45.                         PrintStudentInfo(p);
  46.                         printf("\nPress Anykey to return..");
  47.                         getch();
  48.                         break;
  49.                 }
  50.                 case '2':{
  51.                         int id;
  52.                         printf("input the num you want to delete:");
  53.                         scanf("%d",&id);
  54.                         flushstdin();
  55.                         if (!delStu(prehead,id)){
  56.                 printf("can't find this student!\n");
  57.                         }
  58.                         else{
  59.                             printf("delete success!\n");       
  60.                         }
  61.                        
  62.                         printf("\nPress Anykey to return..");
  63.                         getch();
  64.                         break;
  65.                 }
  66.                 case '3':{
  67.                         int id;
  68.                         printf("input the num you want to find:");
  69.                         scanf("%d",&id);
  70.                         flushstdin();
  71.                          struct students *p=searchStu(prehead,id);
  72.                          if (p){
  73.                                  PrintStudentInfo(p);
  74.                         }
  75.                         else{
  76.                                 printf("Can't find this student!");
  77.                         }
  78.                         printf("\nPress Anykey to return..");
  79.                         getch();
  80.                         break;
  81.                 }
  82.                 case '4':
  83.                         showList(prehead);
  84.                         printf("\nPress Anykey to return..");
  85.                         getch();
  86.                         break;
  87.                 case '0':
  88.                         break;
  89.                 default:
  90.                         printf("Unrecognized operation!");
  91.                         getch();
  92.                         return -1;
  93.         }
  94.         return op;
  95. }

  96. int main(){
  97.         int op,i;
  98.         InitAll();
  99.         do{
  100.                 system("cls");
  101.                 PrintStartInterface();
  102.                 }while(PostOperation());
  103.         return 0;
  104. }

  105. struct students* CreateStudent(){
  106.         struct students *s=(struct students*)malloc(sizeof(struct students));
  107.         if (s==NULL) {
  108.                 printf("menory allocation error!");
  109.         exit(1);
  110.         }
  111.         printf("input the student information:\n");
  112.     printf("\n   input students's name :");
  113.     scanf("%s",s->name);
  114.     flushstdin();
  115.     printf("\n   input students's num and score:");
  116.     setbuf(stdin,NULL);
  117.     scanf("%d%f",&s->num,&s->score);
  118.     flushstdin();
  119.    
  120.     return s;
  121. }

  122. void addStu(struct students *prehead,struct students* s){
  123.         struct students *p=prehead;
  124.         while (p->next!=NULL && p->next->num<s->num){
  125.                 p=p->next;
  126.         }
  127.         struct students *tmp=p->next;
  128.         p->next=s;
  129.         s->next=tmp;
  130. }

  131. struct students* searchStu(struct students *S1,int id){
  132.     struct students *p=prehead->next;
  133.     while (p!=NULL && p->num<id){
  134.               p=p->next;
  135.         }
  136.         if (p==NULL || p->num>id){
  137.                 return NULL;
  138.         }
  139.         return p;
  140. }

  141. int delStu(struct students *prehead,int id){
  142.     struct students *p=prehead;
  143.     while (p->next!=NULL && p->next->num<id){
  144.               p=p->next;
  145.         }
  146.         if (p->next==NULL || p->next->num>id){
  147.                 return 0;
  148.         }
  149.         struct students* tmp=p->next;
  150.         p->next=tmp->next;
  151.         free(tmp);
  152.     return id;
  153. }

  154. void showList(struct students *prehead){
  155.     if(prehead->next == NULL){
  156.         printf("no student in this list!\n");
  157.         return;
  158.     }
  159.   
  160.     printf("name\tnum\tscore\t\n");
  161.     struct students *p=prehead->next;
  162.     while(p != NULL){
  163.         PrintStudentInfo(p);
  164.         p = p->next;
  165.     }  
  166. }

  167. void PrintStudentInfo(struct students *p){
  168.         if (p==NULL) {
  169.                 printf("null student error!");
  170.                 return;
  171.         }
  172.         printf("%s\t%d\t%.1f\t\n",p->name,p->num,p->score);
  173. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-3-31 22:11:21 | 显示全部楼层
Croper 发表于 2019-3-31 14:27
知道你的意思,你是想move改变的时候(*S1)同时改变是么,

原来如此,我纠结了这么久的问题终于解开了,谢谢大佬
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-3-31 22:53:09 | 显示全部楼层
Croper 发表于 2019-3-31 16:23
我按你的风格重写了一个,自己对照着改改吧

这里还是有一个小bug,当你打印链表的时候,不知道为什么,你的score就是零

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-3-31 23:13:57 | 显示全部楼层
cookies945 发表于 2019-3-31 22:53
这里还是有一个小bug,当你打印链表的时候,不知道为什么,你的score就是零


                               
登录/注册后可看大图

有问题么,为什么我运行起来正常呢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-3-31 23:15:35 | 显示全部楼层
不过
  1.       case '0':
  2.              break;
复制代码

应该改成
  1.           case '0':
  2.                  return 0;

复制代码

不然无法正常退出
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-4-1 21:56:29 | 显示全部楼层
Croper 发表于 2019-3-31 23:15
不过
应该改成
不然无法正常退出

哦好的我知道了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-4-1 21:57:09 | 显示全部楼层
Croper 发表于 2019-3-31 23:13
有问题么,为什么我运行起来正常呢

我说的是我自己写的程序,一开始还没有这个问题的,改着改着就出来了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-4-1 22:09:43 | 显示全部楼层
cookies945 发表于 2019-4-1 21:57
我说的是我自己写的程序,一开始还没有这个问题的,改着改着就出来了
  1.         printf("%s\t%d\t%d\t\n",move->name,move->num,move->score);
复制代码
你的score是浮点变量,输出却是%d
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-4-3 21:41:18 | 显示全部楼层
Croper 发表于 2019-4-1 22:09
你的score是浮点变量,输出却是%d

好的吧,我的小错误还是那么多,谢谢叫你那么耐心一一解答
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-7 12:48

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表