0

In C we have two dimensional arrays, i.e. a[m][n].

In one dimensional arrays a is a pointer to the start of the array.

What about two dimensional arrays? Does a[i] hold a pointer to the start of the i row in an array? And thus a[i] is an array of pointers that is passed to a function in the following matter function(int **a, m, n)?

Dean
  • 7,814
  • 8
  • 30
  • 31

2 Answers2

2

Does a[i] hold a pointer to the start of the i row in an array?

No. The data of a 2D array in C is a contiguous block of elements plus some clever indexing access. But a 2D array is an array of arrays, not an array of pointers.

Formally, the a[i] holds a 1D array. This may decay to a pointer to the first element of the ith row in certain contexts, but its type is still T[n], for some type T that you have not specified.

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • 1D array of pointers, each points to the first element of the 'second level' arrays. – Dean Feb 16 '15 at 11:33
  • @Dean Yes, what about it? What on earth are you trying to say? A black dog on a hill. – juanchopanza Feb 16 '15 at 11:49
  • I wanted to get is straight what each array contains, it's perfectly clear from the answer now, thanks. – Dean Feb 16 '15 at 12:00
  • @Dean Each array contains `i` number of items of the given type, no more, no less. There are no pointers anywhere. (Unless of course you declare an array of "pointers-to-type".) – Lundin Feb 16 '15 at 12:02
  • Unfortunately during a C course they presented a multidimensional array as an array of pointers, which point to the first elements of array, hence the confusion. – Dean Feb 16 '15 at 12:21
  • @Dean Are you sure it wasn't in fact an array of pointers? You can actually have an array of pointers, where every pointer *can* be made to point to another array. But if that wasn't the case, it is a terrible way to explain 2D arrays! – juanchopanza Feb 16 '15 at 12:23
  • Pretty sure. I believe they meant that it acts in similar fashion to an array of pointers, and it simply wasn't stressed enough. – Dean Feb 16 '15 at 12:29
1

In one dimensional arrays a is a pointer to the start of the array.

Not correct. a is an array. When you use a in an expression, it "decays" into a pointer to the first element. To better understand this, read this chapter of the C FAQ, particularly this one.

What about two dimensional arrays? Does a[i] hold a pointer to the start of the i row in an array?

No. In a 2D array, a[i] is an array, while int a[x][y]; is an array of arrays. There are no pointers anywhere.

You might be confused because C allows this syntax: int a[][N] = ...;, but that syntax merely means that the size of the array of arrays depends on the number of items in the initialization list.

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • "When you use a in an expression". Surely it depends on the expression. For example, if the expression is the RHS of an assignment to a pointer to array, then it doesn't decay. – juanchopanza Feb 16 '15 at 12:24
  • @juanchopanza Well, yes, but it doesn't seem very helpful to drag all the details of that into this answer, given that the OP is a beginner. – Lundin Feb 16 '15 at 12:32
  • Shouldn't a[x][y] be an element, not an array of arrays? – rcgldr Feb 16 '15 at 15:31
  • @rcgldr Aah well yeah good point. It depends on the context. `int a[x][y];` is an array of arrays, while `a[x][y] = 0;` indeed refers to one item of the array. Edited my answer for clarification – Lundin Feb 16 '15 at 15:33