34th lesson multidimensional arrays and multidimensional pointers

Source: Internet
Author: User

1. pointers pointing to pointers

(1) The nature of the pointer is a variable that takes up a certain amount of memory space

(2) pointer can be defined to hold the address value of the pointer variable

(3) The pointer is a variable, and there is also a value call and a call to the address

"Instance analysis" traversing a two-dimensional array

2. Array name

(1) One-dimensional array name represents the address of the first element of the array: int a[5];a is of type int*

(2) The two-dimensional array name also represents the address of the first element of the array: an int (*) a[3][5],a is of type int (*) [5].

Fetch address (&)

sizeof

array name: A

① two-dimensional array name a point to address of the first element of the array , which is the address of line 1th (A[0]) (note, not the address of a[0][0]). Therefore, A is called the row pointer , which points to the address of the 1th row element (one-dimensional array). The type of this element is Int (*) [5] (that is, a one-dimensional array). So the type of a is int (*) [5];a can be treated as a row pointer, ②a + 1 is a pointer to the second row, A + I represents a pointer to the line i + 1 (also int (*) [5] type) ...

&a represents the address of the entire two-dimensional array , so &a + 1 points to the back of the last element of the two-dimensional array.

 ,

①sizeof (a) represents the size of the entire two-dimensional array.

②sizeof (&a) is a pointer size of 4 bytes.

③sizeof (*&a) equals sizeof (a)

a[i]

①a[i] Type: If you think of a two-dimensional array as an array of three elements (one-dimensional arrays), then these three the a[0], a[1], a[2] , respectively. Thus, a[0] can be thought of as the address of the first element (A[0][0]) to line 1th (one-dimensional array), A[i] is the address of the first element of line I + 1 (one-dimensional array), so A[i] is a type int* .

②a[i] + 1 represents the address of the 2nd element of this row of arrays, i.e. A[i] + 1 is the address of the a[i][1] element, * (A[i] + 1) is the value of the a[i][1] element. Similarly, A[i] + j is the address that points to a[i][j], * (A[i] + j) is the value of a[i][j].

&a[i] The represents the line I + 1 line address of the entire one-dimensional array . Therefore, &a[i] + 1 is a pointer to the next line of this row array.

①sizeof (A[i]): A[i] is a one-dimensional array. sizeof (A[i]) is the size of this array.

②sizeof (&a[i]) is a pointer size of 4 bytes.

③sizeof (*&a[i]) equals sizeof (A[i])

a[i][j]

int .

&a[i][j] represents this , that is, int*

①sizeof (A[i][j]): A[i][j] Represents the type of the element.

②sizeof (&a[i][j]) is the pointer size.

③sizeof (*&a[i][j]) equals sizeof (A[i][j])

Note

① Typically, the array name can be thought of as the address of the first element, whereas the type of the array name A, a[i] in the table refers to the type when they represent the first element of the respective array.

② but when an array name is taken to the address character (&) or sizeof, it cannot be considered as the address of the first element, but rather as the whole . Please note the analysis of the & and sizeof two columns in the table.

Information for the "Instance Analysis" array

#include <stdio.h>intMainintargcChar* argv[],Char*env[]) {        inta[3][5] = {0}; intC; printf ("Information for array:a[3][5]:\n"); printf ("a = 0x%08X, a + 1 = 0x%08X, sizeof (a) =%d\n", A, A +1,sizeof(a)); printf ("&a = 0x%08X, &a + 1 = 0x%08X, sizeof (&a) =%d, sizeof (*&a) =%d\n",               &a, &a +1,sizeof(&a),sizeof(*&a)); printf ("\ n");
A[i] points to the first element of a one-dimensional array,a[i]+1 points to the 2nd element of the row . sizeof (A[i]) cannot be considered as the first element , but the entire one-dimensional array of this line for(c=0;c<5; C + +) {printf ("a[%d] = 0x%08X, a[%d] + 1 = 0x%08X, sizeof (a[%d]) =%d,\n", C, A[c], C, A[c]+1. cnsizeof(A[c])); } printf ("\ n");
//A[i] cannot be regarded as the first element of this line , but rather as an entire one-dimensional array when the a[i is taken for the & address character. That is &a[i] represents the entire array of i+1
&a[i]+1 represents the next line.
 for(c=0;c<5; C + +) {printf ("&a[%d] = 0x%08X, &a[%d] + 1 = 0x%08X, sizeof (&a[%d]) =%d, sizeof (*&a[%d]) =%d\n", C,&a[c],c, &a[c] +1. cnsizeof(&a[c]), C,sizeof(*&a[c])); }    return 0;}/* output result: Information for array:a[3][5]:a = 0x0023fe80, a + 1 = 0x0023fe94, sizeof (a) = 60&a = 0x0023fe80, &a + 1 = 0x0023febc, sizeof (&A) = 4, sizeof (*&A) = 60a[0] = 0x0023fe80, a[0] + 1 = 0x0023fe84, S Izeof (a[0]) = 20,a[1] = 0x0023fe94, a[1] + 1 = 0x0023fe98, sizeof (a[1]) = 20,a[2] = 0x0023fea8, a[2] + 1 = 0x0023feac, siz EOF (a[2]) = 20,a[3] = 0X0023FEBC, a[3] + 1 = 0x0023fec0, sizeof (a[3]) = 20,a[4] = 0x0023fed0, a[4] + 1 = 0x0023fed4, Sizeo  F (a[4]) = 20,&a[0] = 0x0023fe80, &a[0] + 1 = 0x0023fe94, sizeof (&a[0]) = 4, sizeof (*&a[0]) = 20&a[1] =  0x0023fe94, &a[1] + 1 = 0x0023fea8, sizeof (&a[1]) = 4, sizeof (*&a[1]) = 20&a[2] = 0x0023fea8, &a[2] + 1 = 0x0023febc, sizeof (&a[2]) = 4, sizeof (*&a[2]) = 20&a[3] = 0X0023FEBC, &a[3] + 1 = 0x0023fed0, sizeof ( &A[3]) = 4, sizeof (*&a[3]) = 20&a[4] = 0x0023fed0, &a[4] + 1 = 0x0023fee4, sizeof (&a[4]) = 4, sizeof (* &A[4]) = */

34th lesson multidimensional arrays and multidimensional pointers

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.