# Differences between int * P  and INT (* q)  in C ++

Source: Internet
Author: User

These two brothers look so similar that they are often confusing. However, careful understanding and screening will find that they are very different.

The former is a pointer array, and the latter is a pointer to an array. In more detail.

Front: pointer array; it is an array with all elements as pointers.
After: array pointer; it can be directly understood as a pointer, but this pointer type is not an int, not a char, but an array of the int  type. (You can look at it together with the function pointer ......)

Int * P  ------ P is a pointer array, each pointing to an int type
INT (* q)  --------- Q is a pointer pointing to an array of int [4.

The two are defined as follows:

Int K;
Cin> K;

Char * P ;
P  = new char [k];
P  = new char [k];

Char (* B) ;
B = new char [k] ;

Is it true that it is still white? For example

`# Include <iostream> using namespace STD; int main () {int * P ; // P is a pointer array, each Pointer Points to an int type data int A = 1, B = 2, c = 3, D = 4; int I; P  = &; P  = & B; P  = & C; P  = & D; int (* q) ; // Q is a pointer, pointing to the int  array // Q  = & A; // error Q is a pointer to the int  array, and & A is an int pointer, therefore, int AA  = {5, 6, 7, 8}; q = & aa; cout <"P value:" <p <Endl; // note, P is not equal to the value of P  cout <"P :" <p  <"A address: "<& A <Endl; cout <" P  address saved value: "<* (p ) <" A value: "<A <Endl; cout <" P  address saved value: "<* (p ) <" B value: "<B <Endl; cout <" P  address saved value: "<* (p ) <" C value: "<C <Endl; cout <" P  address saved value: "<* (p ) <" D value: "<D <Endl; cout <" Q value: "<q <" AA address: "<& aa <Endl; cout <"Q [I] address:" <Endl; for (I = 0; I <4; ++ I) cout <q [I] <Endl; // Q  has the same value as Q. cout <"Q points to all values of int [4: "<Endl; for (I = 0; I <4; I ++) cout <q  [I] <''; cout <Endl; // cout <* (p ) <* (Q ) <Endl; return 0 ;}`

Running result:

Value of P: 0x22ff60
Value of P : 0x22ff5c a address: 0x22ff5c
P  address saved value: 1 A value: 1
P  address saved value: 2 B value: 2
P  address saved value: 3 C value: 3
P  address saved value: 4 D value: 4
Q value: 0x22ff30 AA address: 0x22ff30
0x22ff30
0x22ff40
0x22ff50
0x22ff60
Q points to all values of int [4:
5 6 7 8

The following is a more detailed explanation from netizens.

The definition involves two operators: "*" (indirect reference), "[]" (subscript), and "[]" with a higher priority.

First, let's look at the priority of int * P  and "[]", so it is first an array of 4, that is, P ; the remaining "int *" is used as a supplement, indicating that each element of the array is a pointer to an integer type. The storage structure of int * P  is as follows: (there is no difference in the horizontal or vertical arrangement of storage squares, as long as they are arranged in the order of memory addresses. This is just convenient for drawing)

Look at int (* q) . First, it is a pointer, that is, * Q, and the remaining "int " as a supplement, that is, the pointer Q points to an array with a length of 4. The storage structure of INT (* q)  is as follows:

See the following definitions:

Int A   = }};

Int C  = {5, 8, 9, 4 };

Int d  = {22, 12, 443 };

Int * P , (* q) ;

Q =;

* P = C;

* (P + 1) = D;

The storage data of int * P  and INT (* q)  is:

Verification:

 # Include Int main (void) {     Int A   = }};    Int C  = {5, 8, 9, 4 };    Int d  = {22, 12, 443 };    Int * P , (* q) ;    Q =;    * P = C;    * (P + 1) = D;     Int I, J;     For (I = 0; I <2; I ++)        For (j = 0; j <4; j ++)        {           If (I = 1) & (j = 3) break;           Printf ("* (p + % d) = % d \ n", I, j, * (p + I) + j ));        }     Puts ("==================== ");     For (I = 0; I <2; I ++)       For (j = 0; j <4; j ++)           Printf ("* (q + % d) = % d \ n", I, j, * (q + I) + j ));   Return 0; }   Output result: * (P + 0) + 0) = 5 * (P + 0) + 1) = 8 * (P + 0) + 2) = 9 * (P + 0) + 3) = 4 * (P + 1) + 0) = 23 * (P + 1) + 1) = 12 * (P + 1) + 2) = 443 ==================== * (Q + 0) + 0) = 2 * (Q + 0) + 1) = 5 * (Q + 0) + 2) = 6 * (Q + 0) + 3) = 8 * (Q + 1) + 0) = 22 * (Q + 1) + 1) = 55 * (Q + 1) + 2) = 66 * (Q + 1) + 3) = 88
Related Keywords:
Related Article

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.