I am getting segmentation fault when I try to pass char array to const char *
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
static char *
get_valid_date_format(const char *date) {
struct tm result;
char *ret;
char **f;
char *formats[] = {"%Y", "%Y-%m", "%y-%m", "%Y-%m-%d", "%y-%m-%d",
"%Y%m%d", "%y%m%d", "%Y-%m-%d %T", "%y-%m-%d %T", "%Y%m%d%H%M%S",
"%y%m%d%H%M%S", "%Y-%m-%dT%T", "%y-%m-%dT%T", "%Y-%m-%dT%TZ",
"%y-%m-%dT%TZ", "%Y-%m-%d %TZ", "%y-%m-%d %TZ", "%Y%m%dT%TZ",
"%y%m%dT%TZ", "%Y%m%d %TZ", "%y%m%d %TZ", NULL };
memset(&result, 0, sizeof(result));
for (f = formats; f && *f; f++)
{
printf("check format: %s\n", *f);
ret = strptime(date, *f, &result);
if (ret && *ret == '\0')
{
printf("found Format: %s\n\n", *f);
return *f;
}
}
return (char *)0;
}
void main()
{
char *format;
char *date = "2020-07-25T00:10:58";
char date2[] = "2020-07-25T00:10:58";
char *date3 = "2020-07-25T00:10:58.000Z";
date2[3] = '1';
format = get_valid_date_format(date2);
if (format == NULL) {
printf("format is NULL\n");
return;
} else {
printf("format found = %s\n", format);
}
}
If I call get_valid_date_format(date), then it works fine. So calling with char * works find but when passing char[] then I get segmentation fault. I need to use array as I need to modify it before calling get_valid_date_format.