1

I'm new with C++ and I started to wonder, what happens if you leave the null character out when defining a char array?

For example, if I define a char array with the null character:

char myarray[] = {'a', 'b', 'c', '\0'};

and then I define it without the null character:

char myarray[] = {'a', 'b', 'c'};

What is the importance of the null character in this scenario? Might the absence of null character in the example above cause some problems later on?...Do you recommend always including or excluding the null character when defining char arrays this way?

Thank you for any help :)

2
  • 4
    The answer entirely depends on what you are going to do next with the char array. It may cause problems if you later treat the array as a C string. Commented Nov 5, 2013 at 11:17
  • 4
    If you're really writing C++, don't do this at all. Use std::string. Commented Nov 5, 2013 at 11:26

6 Answers 6

8

It means that anything that takes a char* as parameter, expecting it to be a null-terminated string, will invoke undefined behaviour, and fail in one way or another*.

Some examples are strlen, the std::string(const char*) constructor, the std::ostream operator<< specialization for char*...

* undefined behaviour means it could even work "correctly", but there is no guarantee this is reproducible.

Sign up to request clarification or add additional context in comments.

4 Comments

Possibly fail, we're in the realm of undefined behaviour.
@john I mean "fail" as in fail to do what you expect it to do, but yes, it is UB and you might not even notice something it wrong.
Exactly. The next byte might happen to be zero, so the code may appear to work some of the time, but a minor change anywhere else can suddenly change the memory layout, and thus a hard to track down strange bug appears.
That's std::string::string( char const* ), not char const&.
3
char myarray[] = {'a', 'b', 'c'};  

If you define with out nul character it is a valid character array not valid string.

1.You should not use this character array as an argument to the string functions like strlen(),strcpy(),etc..

2.You should not print this as we print string with %s in C.

3.You can print character by character.

4.You can compare character by character.

Further char myarray[] = {'a', 'b', 'c', '\0'}; is equal to "abc"

but char myarray[] = {'a', 'b', 'c'}; is not equal to "abc"

Comments

2

If you don't have the terminating null-character, you can still use your char array like you could with the null-char. However, functions that expect null-terminated strings (like strlen), will not stop at the end, since they don't know where the end is. (That's what the null-char is for) They will therefore continue to work in memory until they either go out of bounds and you get a segmentation fault or run until they find their null-char.

Basically, if you want your char array to be a string, append a null-char to denote the end.

Comments

2

what happens if you leave the null character out when defining a char array?

You get an array containing just the characters you specify.

What is the importance of the null character in this scenario?

In C, it's conventional to represent a string as a null-terminated character array. This convention is sometimes used in C++ to interoperate with C-style interfaces, or to work with string literals (which inherited their specification from C), or because the programmer thinks it's a good idea for some reason. If you're going to do this, then obviously you'll need to terminate all the arrays you want to interpret as strings.

The question seems to be about C++, although you've also tagged it C for some reason. In C++, you usually want to use std::string to manage strings for you. Life is too short for messing around with low-level arrays and pointers.

Might the absence of null character in the example above cause some problems later on?

If you pass a non-terminated array to a function expecting a terminated array, then it will stomp off the end of the array causing undefined behaviour.

Do you recommend always including or excluding the null character when defining char arrays this way?

I recommend understanding what the array is supposed to be used for, and include the terminator if it's supposed to be a C-style string.

Comments

1

What is the importance of the null character in this scenario?

High.

Might the absence of null character in the example above cause some problems later on?

Yes. C and C++ functions taking a char* that points to this C-string will require it to be null-terminated.

Do you recommend always including or excluding the null character when defining char arrays this way?

Neither. I recommend using std::string, since you said you are writing C++.

Comments

1

null character will be used by strlen like functions if you wish to use your array as a string. If I need a string because I want to use some text I write:

const char* mystr = "abc"; // it is already null terminated

writing:

char myarray[] = {'a', 'b', 'c', '\0'};

is to verbose

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.