clarification for understanding one line of code

So I picked up this example from this website itself and it helped me be able to copy a string into a char array like I wanted to. However, I do not understand the last line.

1
2
3
4
5
6
    string name;
    name = "Vane";
    char ch[30];
    std::size_t length = name.copy(ch,name.length(),0); 
    cout << length << endl ; //this prints out 4
    ch[length]= '\0';


Can someone please explain to a noob how does:
ch[length]= '\0'; work? What exactly is this line doing, because to me it appears that it is just deleting my entire array. Or is it working in conjunction with a previous line that I perhaps did not fully understand?
Hi,

C char arrays are terminated with a null character - this helps the system find the end. Perhaps it should be :

ch[++length]= '\0';

so the null is at the end, and not overwriting the last char 'e'

The ++ operator at the front of a variable name increments it before it is used.

C++ std::strings are different - they are a class and have a length property, so there is no need for null termination.

Hope all goes well
TheIdeasMan,

The snippet works just fine, my question was why does it work?
Should ch[length] = '\0'; make my entire array null? Or what exactly is it doing?
Since my ch[30] is too many spaces to fit only 4 characters, that last line seems to clean it up for me but I want to understand the logic behind that. If you will notice, my string was just four characters long, but my array was had 30 slots.

In the end my array has as many slots as there are characters in my string.
closed account (48T7M4Gy)
C-style strings, which is whart these are called, are terminated with a null character which is '\0'. The line of code only refers to the last character not all of the array.

The terminator specifies the end of the string. Like a transparent full stop.
Last edited on
So basically what you are saying is that it begins to nullify everything afterthe characters: 'V', 'a', 'n', 'e', ... ?

So my array after line 6 would look like this: 'V', 'a', 'n', 'e', '\0', '\0', etc...
Instead of??
I believe when the copy assignment is called it would copy until it hits the '\0' character. So as it copied, it counted the characters up to the end character. And since arrays start at 0, the char[length] = '\0' would be setting the char array to a proper C-string for in case of other uses for the string.

http://www.cplusplus.com/reference/string/string/copy/

Here it states in the string::copy documentation that it does not append the null character ('\0').
closed account (48T7M4Gy)
No, a c-style string is V-a-n-e-'\0'

ch[0] = 'V'
ch[1] = 'a'
ch[2] = 'n'
ch[3] = 'e'

and
ch[4] = '\0'

There is only one null terminator produced by char[length] = '\0'.

Everything after that is junk, unused but in an allocated 30 characters memory.

You can put nulls in the other unused parts of the array if you want but char[length] is not the way to do it. You'll need a loop because char[length] only refers to one spot.

Try it, and print out ch[length + 1] . See what happens.

@kemort
If putting '\0' in the location char[length] not the way to do it, then why does the sample produced by the cplusplus reference for string::copy use it and not use a 'proper way'?
kemort did not say char[length]='\0' was wrong, just that it will not fill the rest of the character array with null characters (i.e., "with regards to putting null characters it the rest of the string, char[length]='\0' is not the way to do it" is what was being said).
closed account (48T7M4Gy)
Have a read of this tutorial on character arrays http://www.cplusplus.com/doc/tutorial/ntcs/

Also the copy function copies the string character by character, not a multiple copy of the same character.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
using namespace std;

main()
{
    string name;

    name = "Vane";
    cout << "1.   name = " << name << endl;

    char ch[30];
    cout << "2.     ch = " << ch << endl;


    std::size_t length = name.copy(ch,name.length(),0);
    cout << "3.     ch = " << ch << endl;
    cout << "4.   name = " << name << endl;
    cout << "5. length = " << length << endl;//this prints out 4

    ch[length]= '\0';

    cout << "6. ch for all 30 characters:" << endl;
    for (int i = 0; i < 30; i++)
        cout << ch[i]  << " ";
}
Last edited on
But when it comes to an array of characters that is going to be acted as a C-string, you use the null character to determine the end of the string. The rest of the array you wouldn't honestly care about. This is why when you cout the C-string it doesn't spit out the random info that was in the array after the null character.
Thank you guys, that really helped.
Topic archived. No new replies allowed.