尝试使用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

它们指的是在mygetMonth()中使用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 */

最佳答案

getMonth返回指向字符串文本的指针。不允许尝试修改它(例如,使用strcat),这会导致未定义的行为。
我强烈建议使用strftime来处理打印的格式化日期和/或时间字符串。这不仅可以将格式化代码简化为一行代码,而且还可以在需要时/如果需要时支持本地化结果。
编辑:如果不能使用strftime,则需要在自己的缓冲区中生成格式化日期,可能使用sprintf

char buffer[256];
static const char *months[] = {
    "January",
    "February",
    /* ... */ ,
    "November",
    "December"
};

sprintf(buffer, "%s %d %d", months[monthnum], day, year);

09-27 05:15