C指针,我做错了什么?

问题描述:

我没有编译错误,但它崩溃上运行时, 这是我的相关代码,首先它的结构:C指针,我做错了什么?

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

,之后有问题的区域:

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

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

+2

请格式化你的问题,我无法理解的事情,因此,不能尽力帮助你。 – Juan 2010-11-15 21:40:31

+0

有人可以编辑帖子吗?谢谢。 – 2010-11-15 21:41:48

+0

你将首先需要一个调试器 - 查明它在哪里崩溃。 – 2010-11-15 21:42:11

您的ptemp变量是一个未初始化的指针。 改为使用malloc分配适当的空间或将其定义为数组。

您必须在fgets之前为ptemp分配内存。

你可以做到这一点无论是动态还是在栈上:

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

char ptemp[80];的与fgets()之前能够帮助错误。或者在fgets()之前做ptemp = (char *)malloc(80*sizeof(*ptemp));

这是一个问题。

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

你告诉编译器ptemp是指向一些字符,但你永远不分配的空间来存储一些字符写进去。也许这应该是:

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

一旦你完成处理ptemp是未初始化的指针,你可能会想改变这个部分还有:

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

现在,你查看第一个字符的字符值,所以要获得1,用户必须输入Ctrl + A,对于2 Ctrl + B等等。获取10会特别有问题,因为这是一个line-feed字符。

我的猜测是,你想要的东西,如:

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

这将让用户真正进入数字12等,在命令。 对于实际使用,您可能希望用strtol之类的东西替换atoi(尽管它提高了处理不良输入的能力等)。

一旦你解决了这个问题,你还想再看看循环的基本结构。几乎所有的循环形式:

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

...几乎保证它不会正常工作(通常会处理的最后一个输入两次)。由于您使用fgets读取字符串,你可能想使用这样的事情,而不是:

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