user225312
user225312

Reputation: 131817

Initializing C char array using curly braces, is it okay to omit null byte '\0'?

So I read that:

char pattern[] = "ould";

is basically the easier way of writing:

char pattern[] = { 'o', 'u', 'l', 'd', '\0' };

I understand that the null character \0 marks the end of a string, but what if I write it like:

char pattern[] = { 'o', 'u', 'l', 'd'}; (without the \0)

It still compiles.

Where would pattern without the \0 cause problems, because it seems to be compile without warnings (-Wall)

Upvotes: 4

Views: 3617

Answers (8)

programmersbook
programmersbook

Reputation: 4114

char string[] = "abcd"; /*is valid at the end is the \0, 5 chars were allocated*/

char str[3] = {'\0'}; /* all are \0 */

strncpy(str, string, 2); /*strcpy doesn't add \0*/

str[3] = '\0'; /* now is everything fine! */

Upvotes: 2

Cosmin
Cosmin

Reputation: 2385

For example getting the length of the string will produce unpredictable results.
strlen(pattern)

Upvotes: 1

Bogatyr
Bogatyr

Reputation: 19343

Of course it compiles -- it's a valid C program. The problems would occur if you tried to pass in your character array to a function expecting a null-terminated string (an array of characters ending with '\0').

Upvotes: 1

Jesse Cohen
Jesse Cohen

Reputation: 4040

it wouldn't cause problems with the compiler, where it would cause problems is that many functions, e.g. printf assume that when you pass something in with the %s format specifier it will be '\0' terminated.

But if you are only passing that array into functions you write, or where you pass in the length of the array as a separate parameter and they all know how long it is it won't cause a problem.

Upvotes: 2

Dave
Dave

Reputation: 3448

It's a problem if ever you treat it as a string. For example, using strcmp, strcpy, printf. They all expect to find the null terminator, so will definitely encounter problems.

A plain old array of characters is OK as long as you're only ever treating it as an array of characters. e.g.

char c = pattern[2];

Upvotes: 1

Daniel A. White
Daniel A. White

Reputation: 191058

Its not a compiler warning, but a logical one. If you use strcpy without the \0 you potentially could corrupt your stack.

Upvotes: 2

CB Bailey
CB Bailey

Reputation: 793239

If you leave off the terminating zero, you no longer have a null terminated string, just an array of char, so passing it to any function that expects a string would be an error. E.g. strlen, as the source parameter to strcpy, as a ... parameter to printf with a %s format specifier, etc.

Upvotes: 12

alternative
alternative

Reputation: 13042

What's wrong with this behaviour? It is perfectly valid to have a char array without a '\0' on the end. Its just an array. The only reason the '\0' exists is to delimit the string. If you know what the length is, you don't need it!

Char arrays aren't special from other arrays. The only thing you need the '\0' for are the standard string functions.

Upvotes: 3

Related Questions