C + + pointer Note One

Source: Internet
Author: User

Differences between left and right values:
An lvalue is a value that can be placed on either side of an assignment operator in an assignment, such as:
int a = 1;
Double b = 2.0
A = b;
b = A;
Both A and B are lvalue, and all variables are lvalue, but the const amount is the exception.
The right value is the value that can only be placed to the right of the assignment operator, such as:
int a = 0;
char *b = "Hello";
3 = A; ERROR
"Howdy" = b//ERROR
Here 3 and "Howdy" are all right values, so you can't put them on the left side of the assignment operator, all constants, characters, and strings are right values.

Declaration and initialization of pointer variables
1. The pointer contains a variable memory address that contains a specific value, and the variable name refers directly to the value, while the pointer is the indirect reference value.
2. A pointer with a value of 0 or null is called a null pointer.
3. The method of declaration is: int *ptr;
4. The pointer operator is &:
int y=5; int *yptr; yptr=&y;
The above statement indicates that the address of the variable y is assigned to the pointer yptr, or it can be said that the pointer yptr points to the variable y
5. Application of the operator:
#include <iostream>
using namespace Std;
int main () {
int A;
int *aptr;
a=7;
aptr=&a;
cout<< "The address of A is" <<&a<<endl;
cout<< "The value of Aptr is" <<aPtr<<endl;
cout<<endl;
cout<< "The value of A is" <<a<<endl;
cout<< "The value of *aptr is" <<*aPtr<<endl;
cout<<endl;
cout<< "Showing that * and & is inverses of each of the other." <<endl;
cout<< "&*aptr =" <<&*aPtr<<endl;
cout<< "*&aptr =" <<*&aPtr<<endl;
return 0;
}
6. function passing by value and passing parameters by pointer
Pass by value
int cuberbyvalue (int);
int main () {
int number=5;
cout<< "The original value of number is" <<number<<endl;
Number = Cuberbyvalue (number);
cout<< "The new value of number is" <<number<<endl;
return 0;
}
int cuberbyvalue (int number) {
return number*number*number;
}
Pass by reference
void cuberbyreference (int *);
int main () {
int number=5;
cout<< "The original value of number is" <<number<<endl;
Cuberbyreference (&number);
cout<< "The new value of number is" <<number<<endl;
return 0;
}
void cuberbyreference (int *nptr) {
*nptr=*nptr* *nptr* *nptr;
}
7.const modifier pointer
A. A very large pointer to a very large amount of data: Maximum permissions, such as int *aptr;
B. A very good pointer to the constant data: that is, you can change the direction of the pointer, but you cannot modify the data through the pointer.
For example, change the pointer direction:
int main () {
const int *APTR;
int x=4,y=5;
aptr=&x;
cout<< "pointer value when pointing to variable x:" <<*aPtr<<endl;
aptr=&y;
cout<< "pointer value when pointing to variable y:" <<*aPtr<<endl;
return 0;
}
Such an operation is allowed
But the data is modified by pointers:
int main () {
const int *APTR;
int x=5;
aptr=&x;
cout<<*aptr<<endl;
x=6;//allow data to be modified
cout<<*aptr<<endl;
*aptr=100;//not allowed
return 0;
}
C. Pointer to a const constant: that is, you can modify the data through the pointer, but you cannot change the pointer's point.
int main () {
int x=5;
int * Const aptr=&x;
cout<<*aptr<<endl;
*aptr=6;//allow data to be modified
cout<<x<<endl;
int y=7;
aptr=&y;//does not allow you to modify the point
return 0;
}
D. Constant pointer to constant data: Minimum permissions, neither modify the pointer direction nor modify the data through pointers;
int main () {
int x,y=5;
const INT * Const aptr=&x;
cout<<*aptr<<endl;
*aptr=6;//not allowed to modify data
aptr=&y;//does not allow you to modify the point
return 0;
}

C + + pointer Note One

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.