I'm trying to concatenate two dynamic c arrays (containing strings) using pointers. I looked up a bunch of things online that use strcat, but I need to learn how to use pointers to do this. I'm not exactly clear on what a dynamic c array is anyway, I just know I have to use "new" for it. Here's my current code that won't compile:
#include <iostream>
using namespace std;
#define MAX_CHAR 50
void append(char*, char*);
int main()
{
char *str1 = new char[MAX_CHAR];
char *add1 = new char[MAX_CHAR];
str1 = "This string";
add1 = " add this one";
append(str1, add1);
cout << str1;
delete [] add1;
delete [] str1;
return 0;
}
void append(char *str, char *add)
{
while(*str != '\0')
str++;
while(*add != '\0')
{
*str = *add;
add++;
str++;
}
*str = '\0';
}
newyou need a correspondingdelete. In your case you needdelete []since you are allocating an array.