尝试使用 C 连接字符串 (*char) 并遇到很多分段错误:
void printDateFormat( char *in ) { /* begin function printDateFormat */
char *month; // month by char
int month_int; // month by digit
char *day; // day by char
char *year; // year by char
char *dateToken; // date token in split
char *formatted; // formatted string
dateToken = strtok (in, "/");
month = &dateToken;
formatted = formatted = getMonth(month);
dateToken = strtok (NULL, "/");
day = &dateToken;
formatted = strcat (formatted, day);
formatted = strcat (formatted, ", ");
dateToken = strtok (NULL, "/");
year = &dateToken;
formatted = strcat (formatted, year);
in = *formatted;
} /* End function printDateFormat */
char *getMonth( int d) { /* begin function *getMonth */
switch (d) {
case 1:
return "January";
// break;
case 2:
return "February";
// break;
case 3:
return "March";
// break;
case 4:
return "April";
// break;
case 5:
return "May";
// break;
case 6:
return "June";
// break;
case 7:
return "July";
// break;
case 8:
return "August";
// break;
case 9:
return "September";
// break;
case 10:
return "October";
// break;
case 11:
return "November";
// break;
case 12:
return "December";
// break;
}
} /* End function *getMonth */
输入 printDateFormat() 应为格式中的另一个字符串:MM/dd/yyyy ... 即。2013 年 3 月 31 日。目的是将其变为:2013 年 3 月 31 日。
编辑:
这是我进入的方式printDateFormat
void option1( void ) { /* begin function option1 */
char date[10]; /*user input date string */
printf("\n\nEnter date [Format: MM/dd/yyyy]: ");
fgets(date, 10, stdin);
scanf("%s", &date);
printDateFormat(date);
printf("\n%s", date);
} /* End function option2 */
编辑2:
好的,做了一些更改,但仍然没有骰子...
这是我的编译器警告:
asgn9.c: In function `printDateFormat':
asgn9.c:224: warning: passing arg 1 of `getMonth' makes integer from pointer without a cast
asgn9.c:237: warning: assignment makes pointer from integer without a cast
他们指的是getMonth()
在我的内部使用printDateFormat()
这是我更新的代码,我仍然在同一个地方遇到分段错误......
void printDateFormat( char *in ) { /* begin function printDateFormat */
char *month; // month by char
int month_int; // month by digit
char *day; // day by char
char *year; // year by char
char *dateTkn; // date token in split
char *formatted; // formatted string
dateTkn = strtok (in, "/");
month = dateTkn;
formatted = getMonth(month);
dateTkn = strtok (NULL, "/");
day = dateTkn;
formatted = strcat (formatted, day);
formatted = strcat (formatted, ", ");
dateTkn = strtok (NULL, "/");
year = dateTkn;
formatted = strcat (formatted, year);
in = *formatted;
} /* End function printDateFormat */
char *getMonth( int d) { /* begin function *getMonth */
static char *months[] = {"January", "February", "March", "April", "May",
"June", "July", "August", "September", "October", "November", "December"};
return strcpy(malloc(32), months[d]);
} /* End function *getMonth */