C ++ pointer 1: pointer

Source: Internet
Author: User

C ++ pointer 1: pointer

The pointer is also a variable that occupies the memory space and is used to save the memory address.

A pointer is also a data type, which refers to the Data Type of memory space caused by it.

Pointer variables and memory blocks it points to are two different concepts

* P operation memory

When the pointer is declared, * indicates that the declared variable is a pointer.

* Indicates the value in the memory space pointed to by the Operation pointer.

* P is equivalent to finding a piece of memory through the address (value of the p variable;

Then the operation memory * p is placed on the left of the equal sign and assigned a value to the memory)

* Put p to the right of the equal sign (get value from memory)

# Include <iostream> using namespace std; # include <stdio. h> void change (int num) {num = 666;} void changeA (int * num) {* num = 666;} void printfSingleArrayInt (int * p, int n) {cout <"/********************************** /"<endl; cout <p <endl; cout <sizeof (p) <endl; // 4 for (int I = 0; I <n; I ++) {cout <* (p + I) <endl ;} cout <"/**********************************/ "<endl ;} void printfSingleArrayIntS (int a [10]) {cout <"/ * **************** S ********************/"<endl; cout <sizeof (a) <endl; // 4 for (int I = 0; I <10; I ++) {cout <* (a + I) <endl ;} cout <"/**********************************/ "<endl ;} int main () {int a [10] = {0}; for (int I = 0; I <10; I ++) {a [I] = I ;} int * p; // the pointer step, which is determined based on the memory space type. Cout <sizeof (p) <endl; // 4 p = a; cout <sizeof (p) <endl; // 4 cout <sizeof () <endl; // 40 cout <sizeof (a + 1) <endl; // 4 cout <sizeof (& a) <endl; // 4 cout <sizeof (& a + 1) <endl; // 4 printfSingleArrayInt (p, 10); printfSingleArrayIntS (); printf ("% d \ n", a, a + 1); printf ("% d \ n", & a, & a + 1 ); return 0;} int main01 () {int num = 100; int * p = & num; cout <& num <endl; // print the num address num = 1; // directly modify the value of num cout <num <endl; // print the value of num * p = 100000; // indirectly modify the num value // * put p on the left of the equal sign to indirectly modify the memory space value/*** p meaning: * is like a key, modify the memory space of the backdoor based on the value of a pointer variable **/cout <num <endl; change (num); cout <num <endl; /* three conditions for indirect value assignment a) two variables (usually one real parameter, one form parameter) B) establish a relationship, and the real parameter access address is assigned to the form parameter pointer c) * The p-type parameter indirectly modifies the value of the real parameter */changeA (p); cout <num <endl; int a = * p; /// * put p on the right of the equal sign and take the cout value from the memory space <a <endl;/* {char * B = NULL; * B = 'J';} */return 0 ;}

Cout <sizeof (a) <endl; // The length of the Data Type contained in the address 40
Cout <sizeof (a + 1) <endl; // 4
Cout <sizeof (& a) <endl; // 4 & a is the address of array a, which occupies four bytes
Cout <sizeof (& a + 1) <endl; // 4 & a + 1 is the address of array a + 1, which occupies four bytes

The printed addresses of the three elements a, a [0], and & a are the same.

A + I indicates that memory address a is used to move the I int address.

    printf("%d %d\n",a,a+1);
    printf("%d %d\n",&a,&a+1);

What are the differences between & a and & a + 1?

& A indicates that the array is regarded as a whole address.

& A + 1 indicates that the array moves an element (array) as a whole ).

Three conditions for indirect assignment

A) two variables (usually one real parameter, one form parameter) B) establish a relationship, and the real parameter access address is assigned to the shape parameter c) * p form parameter indirectly modifies the value of the real Parameter

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.