Understanding the basics of C ++ pointers

Source: Internet
Author: User

The C ++ pointer is like other variables. The difference is that the common variables contain actual data, while the pointer is an indicator, these are some basic issues of C ++ pointers and are also a very important concept in C ++.

It tells the program where data can be found in the memory. This is a very important concept. Many programs and algorithms are designed around pointers, such as linked lists. How to define a pointer? Just like defining another variable, you need to add an asterisk before the pointer name.

Let's look at an example: The following program defines two pointers, all pointing to integer data. Have you noticed the "p" prefix before the two variable names? This is a habit of programmers when defining the basics of C ++ pointers to improve the readability of convenience programs, indicating that this is a pointer. Now let's initialize these two pointers:

 
 
  1. # Include<Iostream. h> 
  2. Void main ()
  3. {
  4. // Declare the variable:
  5. Int nNumber;
  6. Int * pPointer;
  7. // Assign values to them now:
  8. NNumber=15;
  9. PPointer= & NNumber;
  10. // Print the nNumber value of the variable:
  11. Cout<<"NNumber is equal :"<< NNumber<<Endl;
  12. // Now use the pointer to change the value of nNumber:
  13. *PPointer=25;
  14. // Prove that nNumber has been changed by the above program
  15. // Print the nNumber value again:
  16. Cout<<"NNumber is equal :"<<NNumber<<Endl;
  17. }

It indicates the address of the variable in the memory instead of the value of the variable. In this example, pNumberOne is equal to the some_number address, so pNumberOne points to some_number. If some_number is used in the program, we can use pNumberOne.

Let's take an example: you will learn a lot in this example. If you have no idea about the basic concepts of C ++ pointers, I suggest you read this example several times, pointers are very complicated, but you will soon master them. This example is used to enhance your understanding of the content described above. It is written in C. Note: The original English version is the code written in C. The translator rewrite all the code with C ++ again, and compiled in dev c ++ and VC ++ !)

 
 
  1. # Include<Iostream. h> 
  2. Void main ()
  3. {
  4. // Declare the variable:
  5. Int nNumber;
  6. Int * pPointer;
  7. // Assign values to them now:
  8. NNumber=15;
  9. PPointer= & NNumber;
  10. // Print the nNumber value of the variable:
  11. Cout<<"NNumber is equal :"<< NNumber<<Endl;
  12. // Now use the pointer to change the value of nNumber:
  13. *PPointer=25;
  14. // Prove that nNumber has been changed by the above program
  15. // Print the nNumber value again:
  16. Cout<<"NNumber is equal :"<<NNumber<<Endl;
  17. }

Read this program, compile and run it, and understand how it works. If you have finished, prepare and start the next section. This program first calls the SomeFunction and creates a variable named nNumber. Then, let the C ++ pointer base point to it. But where is the problem? When the function ends, the nNumber is deleted because of this local variable. After the execution of the function that defines a local variable, it is automatically deleted by the system.

That is to say, when the SomeFunction returns the main function main), this variable has been deleted, but pPointer also points to the region where the variable has been used but is not in this program. If you still don't understand it, you can read it again.

Pay attention to its local variables and global variables. These concepts are very important. But how can this problem be solved? The answer is dynamic allocation technology. Note that this is different in C and C ++. Most programmers use C ++, so I use the frequently-used title in C ++.

  1. Differences between standard input implementation methods in C and C ++
  2. How does the C ++ compiler allocate storage space for Const constants?
  3. Basic Conception and method of C ++ Class Library Design
  4. Several Methods for converting C ++ Language
  5. How to better compile C ++ code

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.