C forever从子函数返回后的循环



我有一个函数parse,我在main中调用,它逐个读取2D数组的费用行,如果任何费用行的总列不等于5,则返回1并打印错误。但它被困在一个永远的循环中。因为我对c很陌生,所以我无法理解它。

int parse (char* exp, char* exp_str) {
char d[] = " ";
char *cpyexp;

strcpy(cpyexp, exp);

printf("cpyexp %s n", cpyexp);
printf("strlen(cpyexp) %lu n", strlen(cpyexp));

if(strlen(cpyexp) != 9)
{
fprintf(stderr, "error1: parse() failedn");
return 1;
}    
return 0; 
}
int main(int argc, char** argv) {

char* file = NULL ;
char  expenses [ROWS][COLS] = {{"CAR,14,10"},{"INS,10,12"}} ;
char  expenses_str [ROWS][17] ;     
int i = 0; 
while(expenses[i][0] != '' ){
if(parse (expenses[i], expenses_str[i]) == 1) {
fprintf(stderr, "error3: parse_instruction() failedn");
return 1;  
}
if (expenses[i]== NULL || expenses[i] == ''){
break; 
}       
i++;
}
}
int parse (char* exp, char* exp_str) {
char d[] = " ";
char *cpyexp  =malloc(strlen(exp)+1)* sizeof(char)) \this is initialized as NULL and hats why getting an error

strcpy(cpyexp, exp);

printf("cpyexp %s n", cpyexp);
printf("strlen(cpyexp) %lu n", strlen(cpyexp));

if(strlen(cpyexp) != 9)
{
fprintf(stderr, "error1: parse() failedn");
return 1;
}    
return 0; 
}
int main(int argc, char** argv) {

char* file = NULL ;
char  expenses [ROWS][COLS] = {{"CAR,14,10"},{"INS,10,12"}} ;
char  expenses_str [ROWS][17] ;     
int i = 0; 
while(expenses[i][0] != '' ){
if(parse (expenses[i], expenses_str[i]) == 1) {
fprintf(stderr, "error3: parse_instruction() failedn");
return 1;  
}
if (expenses[i]== NULL || expenses[i] == ''){
break; 
}       
i++;
}
}

最新更新