Две проблемы:
- Ваша строка слишком короткая. Фактически, это всего лишь 1 байт в длину и вмещает только нулевой терминатор.
- Это автоматическая c переменная хранения, и она перестает существовать, когда функция возвращается.
- strncat не будет увеличивать строку.
Пример:
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
const char *hundreds[] = {"C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM" };
const char *tens[] = {"X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC", };
const char *digits[] = {"I","II","III","IV","V","VI","VII","VIII","IX"};
char *solution(int n);
int main()
{
char *p;
printf("%s", p = solution(2253));
free(p);
return 0;
}
char *solution(int n)
{
char *resPtr = malloc(n / 1000 + 4 + 4 + 4 + 1);
size_t pos = 0;
if(resPtr)
{
while(n >= 1000)
{
resPtr[pos++] = 'M'; // no need of expensive strncat function
n -= 1000;
}
if(n / 100)
{
strcpy(&resPtr[pos], hundreds[n / 100 - 1]);
pos += strlen(hundreds[n / 100 - 1]);
n = n % 100;
}
if(n / 10)
{
strcpy(&resPtr[pos], tens[n / 10 - 1]);
pos += strlen(tens[n / 10 - 1]);
}
n = n % 10;
if(n) strcpy(&resPtr[pos], digits[n - 1]);
}
return resPtr;
}
https://godbolt.org/z/1sadrb