C指针,我做错了什么?

k-m*_*man -1 c pointers runtime-error

我没有编译错误,但它在运行时崩溃,这是我的相关代码,首先是它的结构:

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

struct Gas_Station *pgasStationHead = NULL;
typedef struct Gas_Station {
   char *name;
   double octan95SS;
 double octan95FS;
 double octan98SS;
 double octan98FS;
 double gasSoldTotal;
 double gasSoldSS;
 double gasSoldFS;
 struct Gas_Station *pgasStationNext;
 struct Client_List *pclientHead;
} Station;

typedef struct Client_List {
   char carID[10];
 char gasType[3];
   double gasAmount;
 char serviceType[12];
 struct Client_List *pclientNext;
} Client;
Run Code Online (Sandbox Code Playgroud)

之后是有问题的区域:

void CommandsSwitch(FILE *input , FILE *output) {

  do {
   int i;
   char *ptemp , *pfuncNum, *pcarID, *pstationName;
   ptemp = fgets(ptemp , 80 , input);
   if (ptemp[0] != '#') {
    pfuncNum = strtok(ptemp , ",");
    i = (int)pfuncNum[0];
    switch (i)
    {
     case 1:
     HowMuchGasPerStation(output);
     break;

     case 2 :
     pstationName = strtok(pstationName , ",");
     AverageGasInSpecieficStation(output , pstationName);
     break;

     case 3 :
     HowMuchGasInAllStations(output);
     break;

     case 4 :
     HowMuchGasFSInAllStations(output);
     break;

     case 5 :
     pcarID = strtok(ptemp , ",");
     HowMuchGasSoldByCarID(output , pcarID);
     break;
     case 6 :
     pcarID = strtok(ptemp , ",");
     pstationName = strtok(pstationName , ",");
     HowMuchGasSoldByStationPerCarID(output , pcarID , pstationName);
     break;
     case 7 :
     pcarID = strtok(ptemp , ",");
     StationsWithClientByCarID(output , pcarID);
     break;
     case 8 :
     pcarID = strtok(ptemp , ",");
     pstationName = strtok(pstationName , ",");
     HowMuchClientSpentByStation(output , pcarID , pstationName);
     break;
     case 9 :
     pcarID = strtok(ptemp , ",");
     HowMuchClientSpentInTotalByCarID(output , pcarID);
     break;

     case 10 :
     pstationName = strtok(pstationName , ",");
     ClientDetailsBySpecieficStation(output , pstationName);
     break;
    }
   }
  }while(!feof(input)); 

 fclose(input);
 fclose(output);
}

int main (int argc, char* argv[]) {
 int i;
 FILE *f , *input , *output;
 for (i = 2; i < argc; i++) {
  f = fopen(argv[i] , "r");
  if (f == NULL) {
   error("can't open file, might not exists");
  }
  else {
   AddStation(f);
   fclose(f);
  }

 }
 if (argv[1] != NULL) {
  input = fopen(argv[1] , "r");
  if (input == NULL) {
   error("can't open file, might not exists");
  }
 }

 output = fopen("result.txt" , "w");
 if (output == NULL) {
   error("can't open file");
 }
 CommandsSwitch(input , output);

return 0;
}`
Run Code Online (Sandbox Code Playgroud)

在CommandSwitch函数中,Call堆栈指向*ptemp,说我不能使用它,因为它没有初始化或者什么......我做错了什么?!

Jen*_*edt 7

您的ptemp变量是未初始化的指针.使用malloc以分配适当的空间或将其定义为一个阵列,来代替.