Pointers and references (3) array pointers and arrays of pointers

Source: Internet
Author: User
Tags array length

1. Knowledge Point 1.1 pointer array-an array that holds pointers

(1) The pointer array is essentially an array, and the pointer is the contents of the array, indicating that each element in the array is a pointer, so the array of pointers is the array holding the pointer. The following is the use of pointer arrays:

1 int Ten  - ; 2 int *p[3]; 3 p[0] = &A; 4 p[2] = &b;

(2) The definition of an array of pointers can be abstracted as: pointer to variable type * array name [array length].

(3) [] The priority is higher than *, so [] and P is first combined, stating that P is an array, length is 3, and its array element is of type int *.

1.2 Array pointers--pointers to arrays

(1) An array pointer is essentially a pointer to the type that the pointer points to, indicating that the pointer is to an array, so the array pointer is a pointer to the array. The following is the use of array pointers:

1 int a[31,2,3  }; 2 int (*p) [3]; 3 p = &a;

(2) The definition of an array pointer can be abstracted as: array element type (* pointer name) [array length].

(3) () so that the * with P first, so p is a pointer, cover off (*P) The rest is the pointer to the content of the int[3].

(4) The array pointer points to the entire array a (take address &a), not to the first element of array a (take address a), although the address of the first element in array A and arrays A is the same.

2. Face Question 2.1 Brief description of the difference between array pointer and two-dimensional array

Please write out the output of the following program (assuming that the address of array a &a to 0x001df720)

1 intMainintargcChar*argv[]) {2     //differences between array pointers and two-dimensional arrays3     inta[2][5] = { {1,2,3,4,5},{6,7,8,9,Ten} };4     int(*p) [5] =A;5cout << P <<Endl;6cout << p +1<<Endl;7 8cout << *p <<Endl;9cout << * (P +1) <<Endl;Tencout << *p +1<<Endl; One  Acout << **p <<Endl; -cout << * * (P +1) <<Endl; -cout << * (*p +1) <<Endl; the  - GetChar (); -     return 0; -}

Knowledge Point tip: (1) The array name is always equivalent to the address of the first element of the array: a<=>&a[0].

(2) When the number of references is equal to the dimensions of the array, the element values of the array can be obtained, such as the two-dimensional array must be dereferenced two times to get the element value of the array.

(3) The address of the first element of the array and the addresses of the arrays, although in the same location, but they are not one thing (understood by the dereference), the first element of the array (&a[0] after the dereference is * (&a[0]) =a[0], is the value of the first element, and the array address &a after the dereference is * (&a) =a=&a[0]; through the above description, the array address once the address of the first element of the array, and then the value of the first element (in the case of a one-dimensional array) after the reference.

(4) Two-dimensional array a[n][m], you can think of a[n] as the inner layer [m] array name.

According to the above knowledge and rules of careful analysis can be obtained the following answers:

1 //the address of array a &a is 0x001df7202  intMainintargcChar*argv[]) {3      inta[2][5] = { {1,2,3,4,5},{6,7,8,9,Ten} };4      int(*p) [5] = A;//A==&a[0], the left and right types are the same, p points to a one-dimensional array with element 55cout << p << Endl;//two times the dereference is the address, p=&a[0],0x001df7206cout << p +1<< Endl;//two times the dereference is the address, p+1=&a[1]=0x001df720+4*5=0x001df7347   8cout << *p << Endl;//One solution reference is address, *p=a[0]=&a[0][0],0x001df7209cout << * (P +1) << Endl;//once the dereference is the address, * (p+1) =a[1]=&a[1][0],0x001df734Tencout << *p +1<< Endl;//One solution reference is address, *p+1=&a[0][1]=0x001df724 One   Acout << **p << Endl;//two times the dereference is the element value, **p=a[0][0]=1 -cout << * * (P +1) << Endl;//two times the dereference is the element value, * * (p+1) =a[1][0]=6 -cout << * (*p +1) << Endl;//two times the dereference is the element value, * (*p+1) =a[0][1]=2 the   - GetChar (); -      return 0; -}
2.2 Brief description of the difference between the array address and the first address

Indicate the error in the following program:

1 intMainintargcChar*argv[]) {2     intb[6] = {1,2,3,4,5,6 };3     int*p = &b; Left and right types do not match4cout << P <<Endl;5 6 GetChar ();7     return 0;8}

Knowledge Points: The address of the first element of the array and the addresses of the arrays are the same, but they are not the same thing (understood by the dereference), the first element of the array (&a[0] after the dereference is * (&a[0]) =a[0], is the value of the first element, and the array address &a after the dereference is * (&a) =a=&a[0]; through the above description, the array address once the address of the first element of the array, and then the value of the first element (in the case of a one-dimensional array) after the reference.

Modify the following two options:

1 // Way One 2 int (*p) [6]=&b; 3 // Mode two 4 int *p=b; // or: int *p=&b[0];
2.3 A brief description of the difference between pointer arrays and pointers to pointers

Writes out the values of four pointer elements in the pointer array str.

Because of the problem, not the correct answer, so slightly.

Pointers and references (3) array pointers and arrays of 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.