Character Array, \0

Viewed 5610

I know, the \0 on the end of the character array is a must if you use the character array with functions who expect \0, like cout, otherwise unexpected random characters appear.

My question is, if i use the character array only in my functions, reading it char by char, do i need to store the \0 at the end?

Also, is it a good idea to fill only characters and leave holes on the array?

Consider the following:

char chars[5];

chars[1] = 15;
chars[2] = 17;
chars[3] = 'c';

//code using the chars[1] and chars[3], but never using the chars
int y = chars[1]+chars[3];
cout << chars[3] << " is " << y;

Does the code above risk unexpected errors?

EDIT: edited the example.

3 Answers
Related