-4

Here is one way of assigning value to an array

char s[2][2];
s[0][0] = 1;
s[0][1] = 2;
s[1][0] = 3;
s[1][1] = 4;
cout << s[0][2];

output of this will be some garbage and it keeps changing. But strange thing happens when I do this

 char s[2][2];
for (int i = 0, j = 0; i <= 2; i++){
    if(i == 2)
    {
        j++;
        i = 0;
    }
    if(j == 2) break;
    cin >> s[j][i];
}
cout << s[0][2];

so as I give input as

1 2 3 4 the output was 3?

First of all shouldn't it complain that we are using array beyond its index and secondly why so much difference?

Cœur
  • 37,241
  • 25
  • 195
  • 267
codeconscious
  • 396
  • 3
  • 15
  • 1
    `s[0][2]` is not an element in your array. Trying to access this element causes [undefined behavior](http://stackoverflow.com/questions/2397984/undefined-unspecified-and-implementation-defined-behavior). Once you enter undefined behavior there are no guaranties, your program can do just about anything. – François Andrieux Jan 06 '17 at 20:47
  • 1
    You have non nul-terminated strings. You access an array after its last element. Sounds like lots of undefined bahaviour. – Gerhardh Jan 06 '17 at 20:47
  • 1
    C++ and C are different languages. Your code is not in their intersection. Tags edited. – John Bollinger Jan 06 '17 at 20:48
  • 1
    @Gerhardh He is printing a single character, not a `char*`. There is no need for a string terminator. – François Andrieux Jan 06 '17 at 20:48
  • "shouldn't it complain that we are using array beyond its index?" No - you can shoot yourself in the foot. – Weather Vane Jan 06 '17 at 20:50
  • @WeatherVane A warning atleast? – codeconscious Jan 06 '17 at 20:51
  • No. The only protection is memory constraints by the OS. Array lengths are only checked when you initialise them with data at the same time as you declare them. Aside: when you have a function argument which explicilty states the array length such as `void fun(int array[10])` then you still cannot determine the length of the array without being passed that data too. – Weather Vane Jan 06 '17 at 20:52
  • Fix the bugs and the mysteries go away. This is why bugs, when found, should be fixed. – David Schwartz Jan 06 '17 at 20:54
  • @FrançoisAndrieux; You are right. My fault. – Gerhardh Jan 06 '17 at 20:54
  • @FrançoisAndrieux C++ requires row-major storage of 2-dimensional arrays, so `s[0][2]` is equivalent to `s[1][0]`. – Barmar Jan 06 '17 at 21:23
  • UB in your code trying to use s[0][2] which is the third element which doesn't belong to the array. – Raindrop7 Jan 06 '17 at 21:35

2 Answers2

4

One of the things that makes C and C++ fast is that it doesn't check if you go past the bounds of an array.

Instead you get undefined behavior. When you trigger this, anything can happen. Your program may crash, it may appear to work properly, or it may generate strange results.

dbush
  • 205,898
  • 23
  • 218
  • 273
1

First of all shouldn't it complain that we are using array beyond its index

No. The standard does not specify that a program should complain if you use an array out of bounds. The standard specifies that the behaviour is undefined.

secondly why so much difference?

The behaviour is undefined. In both cases.

eerorika
  • 232,697
  • 12
  • 197
  • 326