Const pointer, array pointer, pointer Array

Source: Internet
Author: User

The meaning of the keyword const In the pointer in different positions:

1. Const int * P;

In this case, it indicates a pointer to a constant,ProgramBut the value of the pointer can be changed, that is, the pointer can point to other data. Just like her, she is like a cold-blooded animal, she is so heartless, no matter how much I love her, how much change she makes, or how others care about her, she doesn't feel it. She only cares about herself. Maybe I should look for another one in my life and end this endless pain.

2. int * const P;

In this case, declare a pointer constant (also called a constant pointer ). The value of the pointer itself is immutable, that is, it cannot point to other data, but the value of the data it points to can be changed. It's like me, I only have her in my heart, and it will never change. You only need to ask who I love most. It must be her. But I don't know her heart. I don't know whether she has me or what she is thinking. Even one day, she leaves me, her heart is filled with others, and I am still so stupid.

3. Const int * const P;

In this case, a pointer constant pointing to a constant is declared. The value of the pointer itself cannot be changed, and the value of the data it points to cannot be changed through the pointer. It's like our old vow. I only have her in my heart. She has only me in her heart. We never give up, let alone, and never let go.

 

Understanding array pointers

# Include <iostream. h> void main () {int A [3] [3] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; int I = 1, j = 1; INT (* P) [3]; P = A; int x = * (p [I] + J); int y = * (p + I) [J]; cout <x <"" <Y <Endl ;}

 

X = 5; y = 7;

Because p is a pointer to an array containing three elements, P = P [0] = & A [0] = & {1, 4, 7 }; P [I] = P [1] = & A [1] = & {2, 5, 7}; P [I] + J = P [1] + 1 = & 5;

P = A [], a [] [] = P []; the priority of square brackets is greater than *, so the essence of Y is a [], X is actually a [] []; y = * (p + I + J) = * (p + 2) = 7;

 

Understanding pointer Arrays

# Include <iostream. h> void main () {int A [5] = {1, 2, 3, 4, 5}; int (* pA) [5] = &; cout <"* pa [0] =" <* pa [0] <Endl; cout <"* (PA [0] + 1) = "<* (PA [0] + 1) <Endl; cout <" * (PA [1]) = "<* (PA [1]) <Endl; char * Pb [5] = {"ABC", "def", "123", "456", "789 "}; cout <"Pb [0] =" <Pb [0] <Endl; cout <"Pb [1] =" <Pb [1] <Endl ;}

* Pa [0] = 1
* (PA [0] + 1) = 2
* (PA [1]) = 2367460
Pb [0] = ABC
Pb [1] = def

Here, because int (* pA) [5]: Pa is an array pointer pointing to int [5 ].

* (PA [1]) = * (PA + 1); this is equivalent to moving the pointer to a grid.

Pa points to an array and treats the array as an element. When the pointer value changes, the value it points to is definitely not the original array.

 

INT (* pA) [5] = &;

If it is a normal pointer, it is the first element in the array that is assigned a value during initialization,

A is the address that represents the starting position of the array, for example, a [0].

The array pointer initialization value must be assigned the location of the entire array in the memory. & A represents the location of the entire array in the memory. In this case, the entire array is regarded as a whole.

 

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.