Bluasul
Bluasul

Reputation: 325

C++: What if theres a null character before any other character in an array?

If we're outputting said array and the first character is \0, is it just ignored and the next character that isn't null treated as the first character?

Upvotes: 0

Views: 95

Answers (3)

starturtle
starturtle

Reputation: 739

By convention, if the "C string" (read char array) uses "string-ish" methods to do something with the string, it will stop at the first ASCII-zero (length parameters are regarded as "up-to" lengths). If it uses "array-ish" methods, it will regard a length parameter as a "precisely" length and iterate a for loop from zero to that size. The latter can often be found in binary handling, while the former is convention for string treatment of char arrays.

Examples in C standard headers are memxyz functions for binary array as opposed to strnxyz functions for strings.

Upvotes: 0

chickenwingpiccolo
chickenwingpiccolo

Reputation: 193

C-styled strings are by default, sentinel character arrays meaning they terminated at the first appearance of \0 (or some form of null), so it shouldn't be ignored. It should terminate the string, treating it as an empty string.

Upvotes: 1

ABuckau
ABuckau

Reputation: 349

Depends on the 'outputting' function...if it knows the length of the array, it could output every element regardless of value. Most functions working with 'C strings' will stop at the first \0.

Upvotes: 3

Related Questions