I am currently reading about default arguments of functions in my c++ book. As an example, it shows a program that concatenates two strings, like this:
Code:
#include <iostream>
#include <cstring>
using namespace std;
void concatenate(char *str1, char *str2, int n=0);
int main()
{
char str1[80] = "hello there.";
char str2[80] = " you are cool";
concatenate(str1, str2);
cout << str1 << ".";
cin.get();
}
void concatenate(char *str1, char *str2, int n)
{
while(*str1) str1++;
if(!n) n = strlen(str2);
while(*str2 && n)
{
*str1 = *str2;
str1++;
str2++;
n--;
}
*str1 = '\0';
}
i have found that removing the final step, which adds the null-termination to the final string, has no effect on the string and it's ability to be manipulated by the program.
why is this?
p.s, when adding the null-termination, what's the reason for the backslash? ' \0 '
thanks A LOT for your help