jjepsuomi
jjepsuomi

Reputation: 4373

The importance of null character when initializing char arrays

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 :)

Upvotes: 1

Views: 3477

Answers (6)

juanchopanza
juanchopanza

Reputation: 227578

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.

Upvotes: 8

Gangadhar
Gangadhar

Reputation: 10526

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"

Upvotes: 3

Mike Seymour
Mike Seymour

Reputation: 254741

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.

Upvotes: 2

marcinj
marcinj

Reputation: 50036

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

Upvotes: 1

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385385

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++.

Upvotes: 1

Xandaros
Xandaros

Reputation: 655

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.

Upvotes: 2

Related Questions