1

After a lot of fight I figured out in a declaration like int A[2][3], A is of a type compatible with int(*)[3] . My understanding is A can be used to point to the first three elements and A + 1 to the next three. Is there any way I can declare an array of pointers to array.

   ptr_array    

    +----+                  +----+----+----+                        
    |    |        ---->     |    |    |    |
    |    |                  | 24 | 25 | 26 | 
    +----+                  +----+----+----+
    |    |                  | 44 | 45 | 46 |
    |    |         ---->    |    |    |    |
    +----+                  +----+----+----+
 Array of pointers          two dimensional array of three integers   
   to array of 3

How do I declare ptr_array, so that

ptr_array[0] = A;

and

ptr_array[1] = A + 1;
4
  • Your understanding is wrong: A is not a pointer at all. It is an array. An array is not a pointer. Commented Jan 2, 2012 at 19:49
  • Sorry I meant a type compatible with .. Meaning I can say for example int (*s)[3] = A , although A is an array Commented Jan 2, 2012 at 19:55
  • Two excellent resources for understanding complex declarations in C: (1) The clockwise/spiral rule: c-faq.com/decl/spiral.anderson.html (2) cdecl: cdecl.org Commented Jan 2, 2012 at 22:46
  • One more: declaration as use, as described in "The Development of the C Language". There's no way to link to the exact section, but you can read an extract at: "Complex C type declarations, the easy way". Commented Jan 3, 2012 at 1:50

4 Answers 4

6

Multidimensional arrays in C are arrays of arrays. The elements of an n-dimensional array are (n-1) dimensional arrays. A[0], for example, is an int [3].

A:    |24|25|26|44|45|46|
A[0]: |24|25|26|
A[1]: |44|45|46|

In certain contexts, an array is converted to a pointer to the first element of the array, but the array is not itself a pointer.

From the C standard, § 6.5.2.1-3:

3 Successive subscript operators designate an element of a multidimensional array object. If E is an n-dimensional array (n≥2) with dimensions i× j×...×k, then E (used as other than an lvalue) is converted to a pointer to an (n − 1)-dimensional array with dimensions j × . . . × k. If the unary * operator is applied to this pointer explicitly, or implicitly as a result of subscripting, the result is the pointed-to (n − 1)-dimensional array, which itself is converted into a pointer if used as other than an lvalue. It follows from this that arrays are stored in row-major order (last subscript varies fastest).

4 EXAMPLE Consider the array object defined by the declaration

int x[3][5];

Here x is a 3 × 5 array of ints; more precisely, x is an array of three element objects, each of which is an array of five ints. In the expression x[i], which is equivalent to (*((x)+(i))), x is first converted to a pointer to the initial array of five ints. Then i is adjusted according to the type of x, which conceptually entails multiplying i by the size of the object to which the pointer points, namely an array of five int objects. The results are added and indirection is applied to yield an array of five ints. When used in the expression x[i][j], that array is in turn converted to a pointer to the first of the ints, so x[i][j] yields an int.

To create a ptr_array as diagrammed:

int (*ptr_array[2])[3]
ptr_array[0] = A;
ptr_array[1] = A+1;
// or:
ptr_array[0] = &A[0];
ptr_array[1] = &A[1];
// or even:
ptr_array[0] = (int(*)[3])A[0];
ptr_array[1] = (int(*)[3])A[1];
// though this last shouldn't be used in production code

Here's how to work out the declaration. From the diagram, ptr_array is an array of size 2.

... ptr_array[2] ...

The elements of the array are pointers

*ptr_array[2]

to arrays of size 3

(*ptr_array[2])[3]

of ints

int (*ptr_array[2])[3]

If you're ever not certain how to declare something like this, you can use cdecl:

cdecl> declare ptr_array as array 2 of pointer to array 3 of int
int (*ptr_array[2])[3]

You can install a command line version of cdecl (if not already installed) on your development computer. The exact method depends on the platform. Check the documentation and web at large.

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

1 Comment

@outis.. Yes int (*ptr_array[2])[3] is exactly what I was looking for! excellent explanation ! learnt something new today.
2

In a declaration like int A[2][3], A is of a pointer to an array of 3 which means A is of type int(*)[3]

No. A is an array, not a pointer. It is of type int [2][3].

How do I declare ptr_array, so that

ptr_array[0] = A;

and

ptr_array[1] = A + 1;

If by A+1 you mean "the second length-3 array", then you simply need to do this:

int (*ptr_array)[3] = A;

An array decays to become a pointer to its first element in most situations. I suggest reading the C FAQ on arrays and pointers: http://c-faq.com/aryptr/index.html.

3 Comments

This is more a comment than an answer
@BlackBear: Answer completed now!
Sorry I meant a type compatible with .. Meaning I can say for example int (*s)[3] = A , although A is an array ... so like you said ptr_array is still not an array. It is a pointer to an array of 3.
-1

Yes. You can declare an array to be of anything you like.

Your declaration

int A[2][3];

defines a two-dimensional array of ints, of size 2x3. A[0] is a pointer to the start of an array of 3 ints, as is A[1]. A[0][0] is the first element in the first array. A[1][2] is the last element of the last array.

3 Comments

&A + sizeof(*int)? o_O Your explanation of what A is is incorrect: in int A[2][3] (not int[2][3] A), there are no pointers. An array is not "just a pointer, really."
I dont think what you said is right. A[0] is a pointer to int. You can verify this with running these statments with gcc with -Wall option int (*ptr)[3] = A[0]; //incompatible pointer type int *int_ptr = A[0]; //no warning
oops, sorry guys. I goofed on that one. Edited to fix.
-1

A[2][3] is more an array than a pointer. There are only some cases you can consider array as pointers. A is an array to 6 integers. A is of type integer array.

You can declare ptr_array[0] as A[0] and ptr_array as A[1].

2 Comments

It's not "more an array than a pointer", it is an array. "A is of type integer array" doesn't really mean anything.
I mean it should be considered as an array rather than treating A as a pointer.

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.