-3

I declare C++ array as follows. Array size is 5 and number of characters are 5. But i have error: initializer-string for array of chars is too long When i change array size to 6 or empty, then no error. Why is that?

#include <iostream>
using namespace std;
int main()
{
   char name[5] = "ABCDE";
   cout<<name;
   return 0;
}
too honest for this site
  • 12,050
  • 4
  • 30
  • 52
Geeth Welagedara
  • 614
  • 1
  • 8
  • 24
  • 3
    `"ABCDE"` is actually 6 characters long. A string terminating character `'\0'` is appended at the end of string literals. – haccks Jun 02 '16 at 15:19
  • At the end of a string there must be a special character that indicates the string is over. This character is `\0` and takes one byte, therefore the length of your array is 6, not 5. – Fabio says Reinstate Monica Jun 02 '16 at 15:19
  • Note that C will allow the array to be initialized without the trailing null, but C++ doesn't (a good thing in my opinion). – Michael Burr Jun 02 '16 at 15:20
  • Also see: http://stackoverflow.com/questions/25442721/initializer-string-for-array-of-chars-is-too-long-c – NathanOliver Jun 02 '16 at 15:23

3 Answers3

2

Because there's a null character at the end, so the size should be 6.

See string literal:

The null character ('\0', L'\0', char16_t(), etc) is always appended to the string literal: thus, a string literal "Hello" is a const char[6] holding the characters 'H', 'e', 'l', 'l', 'o', and '\0'.

And when initialize character arrays,

If the number of initializer clauses exceeds the number of members and bases (since C++17) to initialize, the program is ill-formed (compiler error)

So compile failed.

And you don't need to specify the size at all, just char name[] = "ABCDE"; will help you to avoid such kind of troubles.

String literals can be used to initialize character arrays. If an array is initialized like char str[] = "foo";, str will contain a copy of the string "foo".

songyuanyao
  • 169,198
  • 16
  • 310
  • 405
1

strings in C are null-terminated. What it means?

It means that they need an extra character to store the null character to indicate the end of the string.

Imagine a function parsing the "ABCDEF", how it knows when the string ended and don't invade other parts of the memory? A: It knows just because the null character '\0'

So that's the reason you need to declare it with 1 extra character in mind.

To exemplify, imagine the piece of code:

#include <stdio.h>

int main() {
        char name[5] = "ABCDE";
        char test[] = "ANYTHING";
        printf("Hello, %s\n", name);
}

What you think it's gonna print? "ABCDE", right?

Actual output:

Hello, ABCDEANYTHING

It happens because printf tries to find the end of the string (the first null character) but only find it when it's already iterating over the test variable in a improper memory access.

It's a "good scenario", in the worst case your program will crash with a buffer overflow and could leave area for security exploitation.

dfranca
  • 5,156
  • 2
  • 32
  • 60
0

The compiler implicitly terminates string literals with the null \0 termination character. So, every string literal you declare actually has an extra \0 character to the end.

char name[6] = "ABCDE";   // <-- Actually [ABCDE\0]
char name[] = "ABCDE";   // <-- Compiler figures it out
WhiZTiM
  • 21,207
  • 4
  • 43
  • 68