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;
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 expressionx[i][j]
, that array is in turn converted to a pointer to the first of the ints, sox[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.
In a declaration like
int A[2][3]
,A
is of a pointer to an array of 3 which meansA
is of typeint(*)[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 .
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.
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].
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.