开发者

C pointers, what am i doing wrong?

i have no compilation errors, but it crashes on run-time, that's my relevant code, first it's structs:

#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;

and after that the problematic area:

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;
}`

in the CommandSwitch function the Call stack points to *ptemp, saying i can't use it because it didnt intialized or something... what am i doing wrong?!


Your ptemp variable is a pointer that is not initialized. Use malloc to allocate appropriate space or define it as an array, instead.


You have to allocate memory for ptemp prior to usage in fgets.

You can do it either dynamically or on the stack:

  • char ptemp[100];
  • char* ptemp = (char*)malloc(100);


Here's one problem.

char *ptemp; 
ptemp = fgets(ptemp , 80 , input); 

You told the compiler that ptemp is a pointer to some characters, but you never allocated space to store some characters to write into. Maybe this should be:

char ptemp[80];
fgets(ptemp, sizeof(ptemp), input);


Doing char ptemp[80]; before the fgets() could help the error. Or do ptemp = (char *)malloc(80*sizeof(*ptemp)); before the fgets().


Once you're done dealing with ptemp being an uninitialized pointer, you'll probably want to change this part as well:

pfuncNum = strtok(ptemp , ",");
i = (int)pfuncNum[0];
switch (i)
{
 case 1:
 // more cases up to `10` elided.

Right now, you're looking at the character value of the first character, so to get the 1, the user would have to enter Ctrl+A, for 2 Ctrl+B, and so on. Getting the 10 would be particularly problematic, because that's a line-feed character.

My guess is that you want something like:

i = atoi(pfuncNum);
switch (i) { 
// ...

This will let the user actually enter the digit 1, 2, etc., in the command. For real use, you probably want to replace atoi with something like strtol though (it improves the ability to deal well with bad input, among other things).

Once you've fixed that, you also want to take a second look at the basic structure of your loop. Almost any loop of the form:

do { 
    /* ... */ 
} while (!feof(input));

...pretty much guarantees that it won't work correctly (will typically process the last input twice). Since you're using fgets to read the string, you probably want to use something like this instead:

while (fgets(...)) {
    /* ... */ 
};
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜