Array pointers and pointer arrays and double pointers

Source: Internet
Author: User

Array pointers and pointers arrays are literally tangled, and sometimes I can't figure out which is an array pointer, which is an array of pointers. This thing is not by memory, we take the analysis and contrast layer stripping method analysis.

1. Conceptual Differences
1.1. Array pointer : The attribute is in front, the subject is behind. That is, the pointer to the array. A pointer that points to an array.
1.2. Array of pointers : The attribute is in front, the subject is behind. That is, an array of pointers. An array in which the elements are pointers.

2. Pointers and arrays of symbols defined separately
2.1 int * p;
Analysis:
The first step: Find the core, the core is p.
The second step: peel-by-layer, p and * binding are pointers. So int*p; it defines a pointer.

2.2. int p[5];
Analysis:
The first step: Find the core, the core is p.
The second step: peel-by-layer, p and [] combine are arrays. so int p[5]; is a definition of an array.

3. Pointer arrays and array pointers define the symbols separately
3.1 int * P[5]
Analysis:
The first step: Find the core, the core is p.
The second step: Delamination, when P first and [] combined or first and *, check the priority table to get [] higher priority. So P first and [] are combined into an array.
Step three: Continue to peel off layers, p[5] This array again and * combined
Fourth step: To get the result, there is an array of 5 elements whose elements are pointers, or arrays of pointers.

3.2. Int (* p) [5];
Analysis:
The first step: Find the core, the core is p.
The second step: peel-by-layer, p and who union? The parentheses have a higher precedence, so p is combined with * to be a pointer,
Step three: Continue to peel each layer, *p this pointer and then combine with the array,
Fourth step: Get the result, there is a pointer to an array, which is an array pointer.

4. Type matching problem for one-dimensional pointer int * and array pointer int (*) []
4.1.
int * p;
int a[5];
p = A; Does the type match?

Don't be afraid, let's analyze it.
(1) p and binding are pointers, and then combined with int, stating that p is a pointer to a variable of type int, type int
(2) A is the name of the array, a rvalue represents the first address of the first element of the array, the first element is the int type, and the first element's address is the int* type.
(3) It is concluded that P and a are of the same type. are of the int* type.

4.2.
int * p;
int a[5];
p = & A; Does the type match?

Let's analyze it again.
(1) p and binding are pointers, and then combined with int, stating that p is a pointer to a variable of type int, type int
(2) A is the name of the array, &a does the right value for the first address of the entire array, which is a pointer to an array, an array pointer of type int (*) []
(3) Step three: Come to the conclusion that the P and &a types do not match, the type of P is the type of int*,&a is int (*) []

5. Two d pointer int * * p1; and pointer array int*p2[5]; (practical use of relatively few, if the use of two-dimensional pointers is definitely used with the pointer array)
5.1. Array of pointers
int *p[5];

Analysis:
The first step: P first and [] combination is an array, and then a combination, that is, the elements inside the array are pointers, that is, the elements in the array, P0,P1,P2,P3,P4, are pointers, what type of array name P is?
The second step: the array name p when the right value represents the first address of the entire array, the entire array is a pointer, that p is the pointer pointer, that is, the type of P is a pointer to the pointer, a double pointer.
important : The double pointer is the pointer array pointer
Analysis ability is very important, than learning the knowledge itself is important, we can never learn all the knowledge, but we could improve the efficiency of learning. Learning itself is more often than learning.


This article is from the "13088918" blog, please be sure to keep this source http://13098918.blog.51cto.com/13088918/1945215

Array pointers and pointer arrays and double 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.