3

I'm currently learning how to manipulate multidimensional array in c

I have the following code

int t[2][2] = {
        {12, 14},
        {16, 18}
};

int * p = t[0];

printf("%d\n", *(p + 2));

And 16 is printed.

But my question is, I'm accessing the third element of the first array that only contained two int elements, is the behaviour for this kind of access undefined?

I understand the behaviour for accessing out of bound index for a single dimensional array is undefined. e.g.

int ar[] = {11, 22};
printf("%d\n", ar[2]); //behaviour undefined

But is it also true for multidimensional array?

halfer
  • 19,824
  • 17
  • 99
  • 186
Thor
  • 9,638
  • 15
  • 62
  • 137
  • 1
    This might be working since addresses are assigned as bunch like [1st][2nd][3rd][4th], if you access p+2 you are getting value at 3rd location which is 16 – best wishes Jan 12 '18 at 03:24
  • 1
    It's not clearly set out in the C Standard whether or not this is allowed. People have been arguing about it without any firm conclusions for the last 30 years – M.M Jan 12 '18 at 04:08
  • 1
    It's mostly agreed that you can write `int *p = (int *)&t;` and then do `p[2]`. – M.M Jan 12 '18 at 04:11
  • @M.M thanks for the heads up! can't believe C standard does not explicit define the behaviour, which I definitly think they should, to avoid confusion :( – Thor Jan 12 '18 at 04:36
  • @M.M: Do you have any helpful references for either side of this argument? My reading (of C11) tends to agree with coderredoc that it’s UB. – Davis Herring Jan 12 '18 at 04:37
  • 2
    The answer to https://stackoverflow.com/questions/43851470/cast-t-to-t answers some part of your question, although the question is not exactly the same. – Ajay Brahmakshatriya Jan 12 '18 at 06:47
  • Does this answer your question? [One-dimensional access to a multidimensional array: is it well-defined behaviour?](https://stackoverflow.com/questions/6290956/one-dimensional-access-to-a-multidimensional-array-is-it-well-defined-behaviour) – Andreas Wenzel Dec 10 '22 at 00:37

3 Answers3

3

C stores a multi-dimensional array as a contiguous memory area. But still what you have done though is explained by what I said earlier in first line , it's still Undefined Behavior.

Strange it may seems but you can read this to check more.

The thing is in C the concept of 2d array is realized by using array of arrays where each element of the array is an array.

So think this way,

p is the decayed pointer to the first array. When you access p[2] though you may get relevant output it is still Undefined behavior.


t is an array each element of which is an array (t[0],t[1]).

Now t[0] is an array which when assigned to p is decayed and points to the first element. Now you access p[2] that is Undefined behavior because you are accessing an array for which valid indices are 0 and 1.


It may seem strange to you - that in C things working doesn't always mean it's defined and backed up by standard. Here on the same note it might seem logical thing to do but it isn't the most defined way to do things.

user2736738
  • 30,591
  • 5
  • 42
  • 56
  • hi coderredoc, thanks for answering my question :) Please correct me if I'm wrong, my current understanding is that since C store multi-dimensional array as a contiguous memory area, we can access elements in the multi-dimensional as if it is one big single-dimensional array? as long as we dont access indexes that are bigger than the total number of elements in the multi-dimensional array? – Thor Jan 12 '18 at 03:29
0

Please understand that p[2][2] is syntactic sugar for array offset calculations, and there really is only a single-dimensional array of contiguous (linear) memory.

It really is more nuanced than that, but that will help you understand as you get started.

You could have declared an array of pointers to arrays of integers, and allocated each separately (or allocate once and calculate pointers), but that comes later.

ChuckCottrill
  • 4,360
  • 2
  • 24
  • 42
0

Multidimensional arrays in c are stored in memory in row major form. Meaning, your example array would be stored as [12,14,16,18]. This means when you access the point in memory of p plus 2 what is really happening is you're going to the very beginning of p in memory and then going 2 times the size of the data type in p past the start. This brings the pointer to the 3rd element in p which is 16.

Anthony
  • 239
  • 1
  • 8