The following is corrupting your memory
input_len = strlen(input);
struct_ptr[j-1].first_field=(char *)malloc(input_len);
...
strcpy(struct_ptr[j - 1].first_field, input);
Instead, allocate +1 for the '\0'
input_len = strlen(input);
struct_ptr[j-1].first_field=(char *)malloc(input_len + 1);
...
strcpy(struct_ptr[j - 1].first_field, input);
Nice that input
is of type size_t
.
Further, the cast is not needed and since you know the length, why not the faster memcpy()
?
input_len = strlen(input);
struct_ptr[j-1].first_field = malloc(input_len + 1);
...
memcpy(struct_ptr[j - 1].first_field, input, input_len + 1);