C pointers, what am i doing wrong? -
i have no compilation errors, crashes on run-time, that's 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 problematic area:
void commandsswitch(file *input , file *output) { { int i; char *ptemp , *pfuncnum, *pcarid, *pstationname; ptemp = fgets(ptemp , 80 , input); if (ptemp[0] != '#') { pfuncnum = strtok(ptemp , ","); = (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; (i = 2; < 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 commandswitch function call stack points *ptemp, saying can't use because didnt intialized or something... doing wrong?!
your ptemp
variable pointer not initialized. use malloc
allocate appropriate space or define array, instead.
Comments
Post a Comment