A
atapi103
I am quite new to C++ and am haveing dificulty with the strncpy
function. I wrote this peice of code but the output I get from the
destination string after the copy is not what I expected, instead it
leaks into the memory space of string 1. Here is the output and the
code, Im hopeing someone can point me in the correct direction to solve
this problem:
const int MAX_STRING = 5;
char str1[] = "Hello, world!";
char str2[MAX_STRING+1]; // include room for null
strncpy(str2, str1, MAX_STRING);
cout << "String1: " << str1 << endl;
cout << "String2: " << str2 << endl;
output: Hello -w 3½-wHello, world!
I was wanting to set string 2 to Hello but instead I got much more then
5 characters.
function. I wrote this peice of code but the output I get from the
destination string after the copy is not what I expected, instead it
leaks into the memory space of string 1. Here is the output and the
code, Im hopeing someone can point me in the correct direction to solve
this problem:
const int MAX_STRING = 5;
char str1[] = "Hello, world!";
char str2[MAX_STRING+1]; // include room for null
strncpy(str2, str1, MAX_STRING);
cout << "String1: " << str1 << endl;
cout << "String2: " << str2 << endl;
output: Hello -w 3½-wHello, world!
I was wanting to set string 2 to Hello but instead I got much more then
5 characters.