Trying to concat strings (*char) using C and having a lot of segmentation faults:
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 */
Input into printDateFormat() is expected as another string in the format: MM/dd/yyyy ... ie. 03/31/2013. Purpose is to turn that into: March 31, 2013.
EDIT:
Here's how I pass into 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 */
EDIT 2:
Ok, made a few changes but still no dice...
here's my compiler warning:
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
they refer to the use of getMonth() within my printDateFormat()
Here's my updated code, I still get a segmentation fault at the same spot...
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 */
Your getMonth returns a pointer to a string literal. Attempting to modify it(e.g., with strcat) isn't allowed -- it leads to undefined behavior.
I'd (strongly) suggest using strftime to handle formatting date and/or time strings for printing. This will not only reduce your formatting code to a one-liner, but also let you support localized results when/if you want as well.
Edit: if you can't use strftime, you'll want to build a formatted date in your own buffer, probably using sprintf:
char buffer[256];
static const char *months[] = {
"January",
"February",
/* ... */ ,
"November",
"December"
};
sprintf(buffer, "%s %d %d", months[monthnum], day, year);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With