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)?

13
  • "In one dimensional arrays a is a pointer to the start of the array." No, it isn't. Commented Feb 16, 2015 at 11:27
  • a[m][n] is an array of arrays. Commented Feb 16, 2015 at 11:27
  • 2
    @MichałSzydłowski: No, a is an array in all cases the OP mentions. "Arrays are not Pointers!" Commented Feb 16, 2015 at 11:29
  • 1
    @Dean: If an array is used without an index it decays to a pointer to its 1st element. Commented Feb 16, 2015 at 11:36
  • 1
    Related: stackoverflow.com/q/12674094/694576 Commented Feb 16, 2015 at 11:54

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

7 Comments

1D array of pointers, each points to the first element of the 'second level' arrays.
@Dean Yes, what about it? What on earth are you trying to say? A black dog on a hill.
I wanted to get is straight what each array contains, it's perfectly clear from the answer now, thanks.
@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".)
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.
|
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.

4 Comments

"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 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.
Shouldn't a[x][y] be an element, not an array of arrays?
@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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.