I'm getting the exact output I want but I keep getting an error that says "Run-Time Check Failure #2 - Stack around the variable 'c' was corrupted." Now I have to for this assignment use strncpy, and strncat and I'm trying to append two strings togethor with the max amount being define with result_maxlength".
I understand I'm overrunning the strncpy, and strncat. And that I need a '\0' to stop the buffer. but I'm still confused on how to exactly fix it. I've had some people say to do result_maxlength-1 but that never solves the issue. Any help would be appreciated.
#include <iostream>
#include <cstring>
using namespace std;
void concat(const char a[ ], const char b[ ], char result[ ], int result_maxlength);
int main()
{
char a[] = "Woozle";
char b[] = "Heffalump";
char c[5];
char d[10];
char e[20];
concat(a, b, c, 5);
concat(a, b, d, 10);
concat(a, b, e, 20);
cout << c << "\n";
cout << d << "\n";
cout << e << "\n";
return 0;
}
void concat(const char a[ ], const char b[ ], char result[ ], int result_maxlength)
{
strncpy (result,a, result_maxlength);
result[result_maxlength] = '\0';
strncat (result, b, result_maxlength);
result[result_maxlength] = '\0';
}