-2
#include <stdio.h>
void main()
{
    int arr[3][2]={2,3,4,5,6,7};

    printf("%d\n",arr);
    printf("%d\n",arr[1]);
    printf("%d",arr[1][2]);
}

The above code when compiled in Borland Turbo C++ gives the output

8682
8686
6

I don't understand how this program works. I understand that while printing arr it returns the base address as 8682 and arr[1] returns next address location 8686 (integer is 4 bytes) but why is arr[1][2] not flashing an error as arr[1][2] is out of bounds?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
sayak
  • 3
  • 2
  • "How this program works" - it doesn't, printing a pointer with `%d` is undefined behavior. Start by changing the format specifier(s) to `%p` when appropriate and casting the pointers to `(void *)`, and then we can speak again. – The Paramagnetic Croissant Aug 22 '14 at 15:31
  • If you want bounds checking, the language is Turbo Pascal. C assumes the programmer knows what they are doing. – Jonathan Leffler Aug 22 '14 at 15:40

3 Answers3

0

In C, errors are not thrown for out of bounds array accesses - in fact it doesn't even make any check! Instead the system will just access whatever happens to be at that spot in memory (one of the major dangers of C programs).

Here's an explanation of what is happening here. Take this code here:

arr[1][2]

What happens internally actually looks like this:

*(arr + 1 * 2 + 2)  // the 2 comes from the second dimension size of the array

The 2D array internally is stored as a one-dimensional array with each row coming after the previous row:

arr[3][2] = {2, 3, 4, 5, 6, 7};
// is the same as
arr[6] = {2, 3, 4, 5, 6, 7};

The math from arr[1][2] works out to accessing arr[5], which is why you get 6 as the value.

Didier Trosset
  • 36,376
  • 13
  • 83
  • 122
robbrit
  • 17,560
  • 4
  • 48
  • 68
0

Strictly speaking, it's undefined behavior. However, if you look at how array indices are treated, you will see why it's working.

arr
|
v
+-----+-----+-----+-----+-----+-----+
|  2  |  3  |  4  |  5  |  6  |  7  |
+-----+-----+-----+-----+-----+-----+

arr[0]
|
v
+-----+-----+-----+-----+-----+-----+
|  2  |  3  |  4  |  5  |  6  |  7  |
+-----+-----+-----+-----+-----+-----+

            arr[1]
            |
            v
+-----+-----+-----+-----+-----+-----+
|  2  |  3  |  4  |  5  |  6  |  7  |
+-----+-----+-----+-----+-----+-----+

               arr[1][0]
               |
               v
+-----+-----+-----+-----+-----+-----+
|  2  |  3  |  4  |  5  |  6  |  7  |
+-----+-----+-----+-----+-----+-----+

                           arr[1][2]
                           |
                           v
+-----+-----+-----+-----+-----+-----+
|  2  |  3  |  4  |  5  |  6  |  7  |
+-----+-----+-----+-----+-----+-----+
R Sahu
  • 204,454
  • 14
  • 159
  • 270
0

why arr[1][2] is not flashing an error as arr[1][2] is out of bound

if my memory serves me right, C wont complain about array out of bounds errors. It will instead simply allow you to go out of bounds.

tgaleman
  • 23
  • 1
  • 4