0

I read that when one inicializes an array it is possitle to use a string literal. But if the list if inicializers is bigger than the size of array, an error is caught.

#include "stdafx.h"
#include <iostream>
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
    char cAr2[3] = "ABC";
    for (int i = 0; i < 3; i++)
        cout<<cAr2[i]<<endl;
    system("pause");

    return 0;
}

Well, this example is given in my book. It really ends like this: error C2117: 'cAr2' : array bounds overflow.

Could you tell me what is what here: I can see an array of 3 elements and 3 elements being placed into it. Everything seems Ok. Why error?

djechlin
  • 59,258
  • 35
  • 162
  • 290
Pepperwork
  • 105
  • 2
  • 4
  • 12

4 Answers4

3

Well, the easy answer is this: if you're going to use an initializer, save yourself some grief and leave out the size.

The longer answer is that strings are null-terminated, which means there's an additional character you do not see at the end of the string. So you will need an array of size n+1 where n is the number of characters you see.

RonaldBarzell
  • 3,822
  • 1
  • 16
  • 23
  • 1
    Summary: Never do work yourself that the compiler can do (it is more accurate than you (and more resilient to change)). – Martin York Jan 15 '13 at 18:25
3

The string literal "ABC" gives you an "array of 4 const char". There are 4 characters because the string is terminated with the null character. That is, your initialisation would be equivalent to:

char cAr2[] = {'A', 'B', 'C', '\0'};

The null character is implicitly appended to the end of your string so that algorithms that loop over the contents of the array know when to stop without having a string length explicitly given.

Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
1

The size 3 is not large enough for the "ABC" string:

 char cAr2[3] = "ABC"; 

You need at least 4 characters to store this string with the null terminator

Even if your compiler auto corrects that (I am not sure), it is not a good idea to undersize the array..

A B
  • 4,068
  • 1
  • 20
  • 23
-2

If you want to initialize using a string literal I think you'll want to do something like this:

char *cAr2 = "ABC";

However, if you'd like to keep the same type do this:

char cAr2[3] = { 'A', 'B', 'C' };
Kevin Tran
  • 80
  • 2
  • -1: No, if anything it would be `char const*`. Populating a brand new array from a string literal, though, is _perfectly reasonable_. What you've done in your answer is to break that approach. – Lightness Races in Orbit Jan 15 '13 at 17:29
  • Your first version gives you a constant value. This is non mutable thus if you pass it to a function that tries to mutate it you get undefined behavior. So you are changing the meaning of the code. The second version here is also still wrong as the string is no longer nul ('\0') terminated which again changes the behavior if it is used in any standard string library. To stick to the original intent you need to increase the size to four and put it in an array. Or not define the size and let the compiler work it our. – Martin York Jan 15 '13 at 18:29