簡體   English   中英

當我的代碼在 c 中執行 printf() 時出現分段錯誤

[英]Segmentation Fault when My Code Executes the printf() in c

下面我發布了我的代碼。 當我編譯時,我沒有收到任何錯誤,只有一個關於我尚未使用的變量的警告。 代碼一直工作到開始打印的代碼行。 我已經測試了所有部分,並且我認為其中一個有問題。 請讓我知道我做錯了什么,以便我修復它。

  #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]);
     // }
  }
 

您傳遞給strtok的唯一第一個參數是NULL 你從來沒有真正給它任何解析。 您的意思是strtok(temp1[i], ","); ?

另外,為什么沒有錯誤檢查? 使用錯誤檢查容易在代碼中找到錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM