Differences between int * P [4] and INT (* q) [4] 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 [4] type. (You can look at it together with the function pointer ......)

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

The two are defined as follows:

Int K;
Cin> K;

Char * P [2];
P [0] = new char [k];
P [1] = new char [k];

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

Is it true that it is still white? For example

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

Running result:

Value of P: 0x22ff60
Value of P [0]: 0x22ff5c a address: 0x22ff5c
P [0] address saved value: 1 A value: 1
P [1] address saved value: 2 B value: 2
P [2] address saved value: 3 C value: 3
P [3] address saved value: 4 D value: 4
Q value: 0x22ff30 AA address: 0x22ff30
Q [I] address:
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 [4] and "[]", so it is first an array of 4, that is, P [4]; 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 [4] 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) [4]. First, it is a pointer, that is, * Q, and the remaining "int [4]" as a supplement, that is, the pointer Q points to an array with a length of 4. The storage structure of INT (* q) [4] is as follows:

See the following definitions:

Int A [2] [4] = }};

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

Int d [3] = {22, 12, 443 };

Int * P [4], (* q) [4];

Q =;

* P = C;

* (P + 1) = D;

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

Verification:

 

 

 

 

# Include <stdio. h>

Int main (void)

{

    Int A [2] [4] = }};

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

   Int d [3] = {22, 12, 443 };

   Int * P [4], (* q) [4];

   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

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.