導き青い星 2021-11-05 11:52 采纳率: 100%
浏览 22
已结题

C语言指针问题 不知道野指针是在哪里出现的,求指点

源代码如下

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct station{
  char name[30]; 
  int is_stop_limited; 
  int is_stop_express; 
  int is_stop_local;  
  struct station *next_station; 
} station;

void make_list(station *list, station *p){
  scanf("%s %d %d %d", list->name, &list->is_stop_limited, &list->is_stop_express, &list->is_stop_local); 
  list->next_station = NULL; 
  while(1){
    char name[30];
    int num1, num2, num3;
    scanf("%s %d %d %d", name, &num1, &num2, &num3);
    if(strcmp(name, "none")== 0){ 
      break;
    }else{
      p = (station *)malloc(sizeof(station));
      p->is_stop_limited = num1;
      p->is_stop_express = num2;
      p->is_stop_local = num3;
      strcpy(p->name, name);
      p->next_station = NULL;
      if(p == NULL){
        printf("error");
        exit(1);
      }
      for(list; list->next_station != NULL; list = list->next_station){ 
      }
      list->next_station = p;
    }
  }
}

void put_station(struct station *stations){
  for(stations; stations != NULL; stations = stations->next_station){
    printf("%s ", stations->name);
  }
  printf("\n");
}

int main(){
  struct station *stations;
  struct station *ptr;
  struct station *begin = stations;
  make_list(stations, ptr);
  put_station(begin);
  free(ptr);
  return 0;
}

在执行后输入第二串数据时make_list最后的for报出了Segmentation fault的错误,虽然感觉是出现了野指针但是不知道具体的位置,求指点

  • 写回答

2条回答 默认 最新

  • CSDN专家-link 2021-11-05 12:10
    关注

    main中指针都没初始化分配空间,make_list函数就scanf住指针的变量里输入数据了。肯定不行了

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 5月27日
  • 已采纳回答 5月19日
  • 创建了问题 11月5日

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line