3

Suppose I declare a 2-D array as:

int a[10][10];

As per my understanding, a is double pointer of type int.

Suppose I declare a pointer to an entire row as follows.

int (*p)[10];

Also a[0] points to the row 0, a[1] points to row 1...and so on.

So I tried to initialise p as

p = a[0];  /* so that p can point to row 0 */

I get a compiler warning of incompatible pointer assignment. But if i write

p = a;

This works fine.

Can someone tell me what I am undertaking wrong here?

Sumit Trehan
  • 3,985
  • 3
  • 27
  • 42
  • 2
    "As per my understanding, a is double pointer of type int" - no, it's an array of arrays. It's not a pointer. It's not a "double pointer". It's not a pointer-to-pointer. It's an **array** (of arrays). As such, `p[0]` and `p[1]` and `p[i]` (for all 0 <= i < 10) are **not pointers either.** They are arrays as well. They decay into pointers of type `int *`. The type `int *` is not compatible with the type `int (*p)[10]`. You are, however, right in that a **pointer** to the i-th row of the array is of type `int (*)[10]`. So you can write this: `int (*row)[10] = &a[0];`. – The Paramagnetic Croissant Jul 04 '14 at 17:15
  • possible duplicate of [C pointer to array/array of pointers disambiguation](http://stackoverflow.com/questions/859634/c-pointer-to-array-array-of-pointers-disambiguation) – The Paramagnetic Croissant Jul 04 '14 at 17:18
  • *"As per my understanding, a is double pointer of type int"* One of the most widespread and phenomenally persistent misunderstanding around. It is crucial that you learn the difference between and array and a pointer and not be fooled by the syntatical ability to interchange the notations. – dmckee --- ex-moderator kitten Jul 04 '14 at 17:21
  • Not a duplicate... I am not concerned with the syntax here alone. – Sumit Trehan Sep 13 '14 at 06:23

4 Answers4

5

As per my understanding, a is double pointer of type int.

You are wrong. a is of type array of arrays of int. Array names converted to pointer to its first element, except when it is an operand of sizeof and unary &.

Also a[0] points to the row 0, a[1] points to row 1..and so on.

No. a[0] points to the first element of row 0 and a[1] points to the first element of row 1 after decay. &a[0] will give you the address of row 0

p = &a[0];  // Equivalent to p = a 
haccks
  • 104,019
  • 25
  • 176
  • 264
1

a is not a double pointer. It's an array of ten arrays of ten ints each. In most uses, it will "decay" into a pointer of type int (*)[10], i.e., a pointer to the first row.

So in the p = a assignment, a decays as described above, so p points to the first row.

If you want to point to another row, you need to explicitly take it's address:

p = &a[5]
ooga
  • 15,423
  • 2
  • 20
  • 21
1

a is double pointer of type int.

No. a is an array of arrays of int.

Also a[0] points to the row 0

No. a[0] is the first row, an array of int. In order to point to the first row, you need to do &a[0].

Most of the time, you can write &a[0] as just a, because an array expression, in most (but not all) contexts, is implicitly converted to a pointer to its first element.

newacct
  • 119,665
  • 29
  • 163
  • 224
  • Another way to think of `&a[0]` is that is effectively the same as `&(*(a+0))`, which is the same as `&*a` and (as adjacent `&*` operators are implicitely reduced) the same `a`. – Grzegorz Szpetkowski Jul 04 '14 at 23:26
0

You can always assign a pointer to an array, string or a function by just using its name. i.e., if a[10] is an array of 10 integer elements you can assign pointer p by using the expression p = a, now 'a' will give the base address of the array. Therefore 'p' now points to an array of 10 elements.

Same concept can be applied for strings and functions..

Liju Thomas
  • 1,054
  • 5
  • 18
  • 25