oddity with charactor arrays..

when a user enters in a character for b[0], it gets assigned to a[0] and c[0]..
someone please explain why

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <cstdio>
#include <cstdlib>

using namespace std;

int main()
{
    start:
    system("cls");
    cin.sync();
    char a[0];
    char b[0];
    char c[0];
    cin >> b[0];
    cout << a[0] << endl;
    cout << b[0] << endl;
    cout << c[0] << endl;
    system("pause");
    goto start;
    return 0;
}


EDIT:: Thanks for all the info. I'm just not used to using charactors that much in my programming, but i understand what you all mean now. And if anyone is interested, i use Code::Blocks with gcc, and it compiles fine, no warnings either. haha
Last edited on
I think your variable definitions need changing. What are you expecting
char a[0];
to do? Why not just use
char a;
?
a, b and c are pointers. They all point to the same location in memory, pointing to arrays of size zero. Your char arrays are of size zero. An array of size zero is forbidden by the C++ standard (see section 8.3.4 of C++ 98)

You can get those memory addresses with the following code to confirm it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <cstdio>
#include <cstdlib>

using namespace std;

int main()
{
    start:
   
    cin.sync();
    char a[0];
    char b[0];
    char c[0];
   
    cin >> b[0];

    cout << a[0] << " address:" << &a << endl;
    cout << b[0] <<  " address:" <<&b << endl;
    cout << c[0] << " address:" << &c << endl;
 
    return 0;
}
Last edited on
That's strange. Metl worf, did that code compile? If it disobeys the standard, then I'd have thought it shouldn't...
It compiles. It's just forbidden :)

I expect that when an array is created, there is a value somewhere keeping track of where the next free memory is. Because the array is of size zero, that value is not moved on, and the next array created starts in the same place.
Last edited on
nothing should have gotten stored there if its an array of size 0. that means it allocated exactly 0 bits so the data isnt getting stored anywhere. im pretty sure in visual studio that wont even compile.
The data will be stored in the memory location the pointer points to. If you build and run the code I posted above, you'll be able to read the memory location it's being stored in. I remind you that if you put five values into an array of length four, the fifth value still gets stored. This is known as a "buffer overrun/overflow".
Last edited on
yes the problem is that your initializing your arrays as size zero as ascii pointed out, try changing them to at least a[1]; when you initialize an array the number in the brackets is the size but when you are putting numbers into the array then b[0] is what you want if your putting it into the beginning of the array.
in visual studio that wont even compile


I tried it. It definitely doesn't compile:
int a[0];
yields
"Cannot allocate an array of constant size zero".
Last edited on
It compiles without too much whinging under Clang version 1.1 and GCC 4.4.3, although if I jacked up the strictness I expect I could make it break :)
Aren't compilers supposed to obey the standard!? Isn't that the whole point? :P
Last edited on
Topic archived. No new replies allowed.