【问题标题】:Segmentation Fault when My Code Executes the printf() in c当我的代码在 c 中执行 printf() 时出现分段错误
【发布时间】:2021-08-16 08:52:58
【问题描述】:

下面我发布了我的代码。当我编译时,我没有收到任何错误,只有一个关于我尚未使用的变量的警告。代码一直工作到开始打印的代码行。我已经测试了所有部分,并且我认为其中一个有问题。请让我知道我做错了什么,以便我修复它。

  #include <stdio.h>
  #include <string.h>
 
  #define NUM_LINES 37
  #define LINE_LENGTH 60
 
  void select_sort_str(char list[NUM_LINES][LINE_LENGTH], int n);
  int alpha_first(char list[NUM_LINES][LINE_LENGTH], int min_sub, int max_sub);
 
 
  int main (void){
  //store each line in an array of strings
      FILE *inp;
      FILE *outp;
      char hurr[NUM_LINES][LINE_LENGTH];
      ;
 
      inp = fopen("hurricanes.csv","r");
      outp = fopen("out.txt","w");
 
 
  //read in lines from file
      for (int i = 0; i<NUM_LINES; i++){
          fgets(hurr[i], LINE_LENGTH, inp);
      }
      inp = fopen("hurricanes.cvs","r");
 
      //printf("%s", hurr[0]);
  //define function
 
 

 
      select_sort_str(hurr, NUM_LINES);
 
 
  return(0);
  }
 
 
  int
  alpha_first(char list[NUM_LINES][LINE_LENGTH],       // input - array of pointers to strings
              int min_sub,        // input - min and max subscripts of
              int max_sub)        //    portion of list to consider
  {
      int first, i;
 
      first = min_sub;
      for (i = min_sub + 1; i <= max_sub; ++i) {
          if (strcmp(list[i], list[first]) < 0) {
              first = i;
          }
      }
      return (first);
  }
 
  /*
   * Orders the pointers in an array list so they access strings in
   * alphabetical order
   * Pre: first n elements of list reference string of uniform case;
   *      n >= 0
   */
  void
  select_sort_str(char list[NUM_LINES][LINE_LENGTH],   // input/output - array of pointers being
                                  // ordered to acces strings alphabetically
                  int n)          // input - number of elements to sort
  {
      int fill,           // index of element to contain next string in order
          index_of_min;   // index of next string in order
      char *temp;
      char temp1[NUM_LINES][LINE_LENGTH];
 
      for (fill = 0; fill < n - 1; ++fill) {
          index_of_min = alpha_first(list, fill, n - 1);
 
          if (index_of_min != fill) {
              temp = list[index_of_min];
              list[index_of_min][LINE_LENGTH] = list[fill][LINE_LENGTH];
              strncpy(temp1[index_of_min], list[index_of_min], LINE_LENGTH);
              temp1[fill][LINE_LENGTH] = *temp;
 
 
          }
      }
      char *name;
      char *cat = 0;
      char *date;
 
    for (int i = 0; i<NUM_LINES; i++){
          name = strtok(NULL, ",");
          cat  = strtok(NULL, "h");
          date = strtok(NULL, " ");
      printf("%s %s %s\n", name, cat, date);
      }
 
     // for( int i =0; i<NUM_LINES; i++){
     //     printf("%s", list[i]);
     // }
  }
 

【问题讨论】:

    标签: c segmentation-fault


    【解决方案1】:

    您传递给strtok 的唯一第一个参数是NULL。你从来没有真正给它任何解析。你的意思是strtok(temp1[i], ",");吗?

    另外,为什么没有错误检查?通过错误检查发现代码中的错误要容易得多

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多