Getting started with pointers in C + +

Source: Internet
Author: User

What is a pointer?

In fact, the pointer is like other variables, the difference is that the general variable contains the actual real data, and the pointer is an indicator, which tells the program where the stored area can find data. This is a very important concept, there are many programs and algorithms are designed around the pointer, such as linked lists.

Start learning

How do you define a pointer? Just like you define a different variable, you just have to add an asterisk to the pointer's first name. Let's look at an example: The following program defines two pointers, all of which point to integer data.

int * pNumberOne;
int * pNumberTwo;

Did you notice the "p" prefix in front of the two variable names? This is what programmers usually do when they define pointers.

A habit to improve the reading of the program, indicating that it is a pointer. Now let's initialize both of these pointers:

pNumberOne = &some_number;
pNumberTwo = &some_other_number;

The & number reads "What's the address", which means that the value of the variable in memory is returned instead of the variable itself. In this case, Pnumberone equals Some_number's address, so now Pnumberone point to Some_number. If we use Some_number in the program now, we can use Pnumberone.

Let's learn an example:

In this example you will learn a lot, if you do not understand the concept of the pointer, I suggest you look at this example, the pointer is a very complex thing, but you will soon grasp it.

This example is used to enhance your understanding of the content described above. It is written in C (Note: The original English version is written in C code, the translator rewrote the C + + write all the code, and in the dev C + + and VC + + compiled through!) )

#include <iostream.h>

void main()
{
// 声明变量:

int nNumber;
int *pPointer;

// 现在给它们赋值:

nNumber = 15;
pPointer = &nNumber;

//打印出变量nNumber的值:

cout<<"nNumber is equal to :"<< nNumber<<endl;

// 现在通过指针改变nNumber的值:

*pPointer = 25;

//证明nNumber已经被上面的程序改变

//重新打印出nNumber的值:

cout << "nNumber is equal to :" << nNumber << endl;
}

Read through the program, compile and run it, and be sure to understand how it works. If you're done, get ready and start the next section.

Trap!

Try it, can you find out the error of the following procedure?

#include <iostream.h>

int *pPointer;

void SomeFunction();

{
int nNumber;
nNumber = 25;

//让指针指向nNumber:

pPointer = &nNumber;
}

void main()
{
SomeFunction(); //为pPointer赋值

//为什么这里失败了?为什么没有得到25

cout<<"Value of *pPointer: "<<*pPointer<<endl;
}

The program first calls the Somefunction function, creates a variable called Nnumber, and then lets the pointer ppointer point to it. But where is the problem? When the function is over, the nnumber is erased,

Because this is a local variable. A local variable is automatically deleted by the system after the function that defines it is executed. That is, when the Somefunction function returns the main function main (), the variable has been deleted, but Ppointer also refers to the area where the variable has been used but is now not part of the program. If you don't understand, you can read the program again, notice its local variables and global variables, these concepts are very important.

But how can we solve this problem? The answer is dynamic allocation technology. Note that this is different in C and C + +. Since most programmers use C + +, I use the commonly used appellation in C + +.

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.