Pointer to array

Source: Internet
Author: User

 
 
 
The array name represents the first address of the array, and then the address of the array, its value is still the same.
Int A [5] = {1, 2, 3, 4, 5}; A, & A their values are the same

Returns the address of an array. The type can be used as a pointer to this array. It is not specified as a pointer.
Int A [5] = {1, 2, 3, 4, 5 };
& Type A is a pointer to a one-dimensional array (five elements), that is, INT (*) [5]
The data in & A + 1 is the address of a + 5 * sizeof (INT)

Int B [3] [4] = {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12 }};
Type & B is a pointer to a two-dimensional array (3*4 elements), that is, INT (*) [3] [4]
& B + 1 data is the address of B + 3*4 * sizeof (INT)

In this example, & array is a pointer, because & (& array name) or & (& array name) are the same as the values of array names, obviously, pointers do not have this feature.

Unreference of pointer to array

3D array, which can be seen as a pointer to a two-dimensional array. After being unreferenced, It is a pointer to a one-dimensional array, that is, a two-dimensional array.
A two-dimensional array can be seen as a pointer to a one-dimensional array. After being unreferenced, It is a pointer to a type (such as an int), that is, a one-dimensional array.
A one-dimensional array can be seen as a pointer to a type (such as INT). After being referenced, It is a number of types (such as INT), that is, a number.

 

Int main (INT argc, char * argv [])
{

Int A [5] = {1, 2, 3, 4, 5 };
Int B [3] [4] = {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12 }};

Printf ("% P/N", & A);/* A takes the address (type as a pointer to a one-dimensional array (with five elements), that is, INT (*) [5]). Get the address of the array. The content is the same as the first address of the array. It is recorded as Pa */
Printf ("% P/N", & A + 1);/* pa + 5 * sizeof (INT), & A points to a one-dimensional array, add 1 to skip 5 elements */
Printf ("/N ");
Printf ("% P/N", a);/* the value of A (the first address of array A, type as int *). The printed value is still Pa */
Printf ("% P/N", A + 1);/* pa + 1 * sizeof (INT), a points to int, plus 1 to skip 1 element */

Printf ("/N ");
Printf ("% P/N", & B);/* B takes the address (type as a pointer to a two-dimensional array (with 3*4 elements), that is, INT (*) [3] [4]), obtains the address of the array. The content is the same as the first address of the array and is recorded as Pb */
Printf ("% P/N", & B + 1);/* Pb + 3*4 * sizeof (INT), & B points to a two-dimensional array, add 1 to skip 3*4 elements */
Printf ("/N ");
Printf ("% P/N", B);/* B value (the first address of array B, the type is considered as int (*) [4]) print the value in the address format as Pb */
Printf ("% P/N", B + 1);/* Pb + 4 * sizeof (INT), B points to a one-dimensional array (4 elements ), add 1 to skip 4 elements */
Printf ("/N ");
Printf ("% P/N", * B);/* the two-dimensional array is referenced as a one-dimensional array * B = * (B + 0) = B [0] */
Printf ("% p/n", * B + 1);/* Add 1 to a one-dimensional array, skip 1 element, that is, add 1 * sizeof (int )*/
Printf ("/n ");
Printf ("% p/n", ** B);/* One-dimensional array returns a number of types ** B = * (B + 0) + 0) = * (B [0] + 0) = B [0] [0] */
Printf ("% p/n", ** B + 1);/* B [0] [0] + 1 = 2 */
Getch ();
Return 0;
}

 

The result is as follows:

 


Int c [3] [4] [5];

The type of & c is (int (*) [3] [4] [5])

The c type is (int (*) [4] [5]),

* C, that is, c [0]. The type is int (*) [5].

* C [0] type is int *

 

Returns a pointer to an array as a form parameter.

3D array, degraded to a pointer to a two-dimensional array
A two-dimensional array degrades to a pointer to a one-dimensional array.
A one-dimensional array that degrades to a pointer to a type (such as an int ).

You can use the following function for testing. Note that when an array is used as a parameter, only the first dimension can be null.

For example, the parameter can be int ss [] [4] Or int ss [2] [3], int ss [] [4] [5]

Test (int ss [] [4])
{
Printf ("% p/n", ss );
Printf ("% p/n", ss + 1 );
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.