C ++ pointer Recognition

Source: Internet
Author: User

I started to understand the pointer from Java to C ++ and talked about my understanding of the pointer. the pointer is also a variable type, and the address of an object or variable is stored, let's start with the code: the code is the most direct:

# Include "stdafx. H "int _ tmain (INT argc, _ tchar * argv []) {int I = 100; // assign a value to an int * P = & I; // you can define a pointer and assign a value in this way. // The Value assignment method is int * PT; Pt = & I;
// * PT = & I cannot be defined like this. The value assignment method is defined like this. // The following is the test result printf ("I: % d \ n", I ); /* // The memory address of a variable pointed to. It can be seen that p = & I is a little different from the value assignment above. At first, I always thought the value assignment method was * P = & I; then * P = & I should be fine; or I may have poor understanding, */printf ("P: % d \ n", P ); printf ("& I: % d \ n", & I); printf ("* P: % d \ n", * P ); // The content or value of the memory address printf ("PT: % d \ n", pt); printf ("* PT: % d \ n", * PT ); /* the pointer memory address, indicating that the pointer also has its own memory address. The pointer is a type */printf ("& PT: % d \ n", & pt ); printf ("& P: % d \ n", & P); Return 0;

The above is my personal understanding of pointers.

Use Pointer:

Int * mypointer = new int;

In this case, the pointer points to an integer address. To access this value, you need to remove the reference from this pointer. The following code

*myPointer = 8;

  


Int * myponiter = new int; // dynamically allocates the memory printf ("& myponiter: % d \ n", & myponiter); printf ("* myponiter: % d \ n ", * myponiter); * myponiter = 8; printf ("& myponiter: % d \ n", & myponiter); printf ("* myponiter 8: % d \ n ", * myponiter); Delete myponiter; // release memory

  

The memory address of the pointer is not changed here, which refers to the content of the memory address changed;

The following code is an understanding of array pointers;

const size_t  size = 5;int int_arr[size] = {0,1,2,3,4};int *b = int_arr;int *e = int_arr+size;printf("b:%d\n",b);printf("b++:%d\n",++b);printf("int_arr0:%d\n",&int_arr[0]);//b = &&int_arr[0]printf("int_arr1:%d\n",&int_arr[1]);//++b=&int_arr[1]printf("e:%d\n",e);for (int *pbegin = int_arr,*end =int_arr+ size;pbegin != end;++pbegin){std::cout<<*pbegin<<endl;}

  

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.