Chapter 4 Pointer pointer and Chapter 4 Pointer

Source: Internet
Author: User

Chapter 4 Pointer pointer and Chapter 4 Pointer

Chapter 3

Pointer

 

The first time I went online for help, I encountered a problem in pointer. For me, sometimes reference and de-reference, address, location, and value are often mixed, even my professor admitted that not only do C ++ beginners encounter such problems in pointer, but some programming experts who have been working for many years will also encounter pointer problems.

 

There is a joke that you can understand when you learn the pointer:

A programmer has just completed the programming of a project, but there is always a bug. When he is thinking about it, the cleaning aunt said, "Young man, leaked ".

 

Here we introduce three new concepts:

 

A new kind of type: reference (also called "address ").

 

An operator (&) to acquire the address of a variable

 

An operator (*) to use the address of a variable

 

For example, in pseudo code

Integer myNum limit 7

Print "the value stored in myNum is", myNum

Print "the address of myNum is", & myNum

 

At this time, the number in the second line will display a string of numbers, so this number is the address

 

Integer myNum // This declares an integer

RefToInteger myNumPtr // This declares a pointer to an integer

 

Before using a variable, We need to declare the type and name of the variable.

 

Integer myNum

RefToInteger myNumPtr

MyNum limit 7

MyNumPtr handle & myNum // This puts the address of myN

// Into the variable myNumPtr

In this case, myNum will contain 7, while the variable

MyNumPtr will contain 4683953 (or whatever address is given to the variable ). In this case, the address may not be the number.

Before assigning values, this pointer is garbage.

 

A valid pointer contains the address of some data.

The pointer "points to" the data.

Following the pointer is called "de-referencing" the pointer.

Problem: dereferencing has 2 related but distinct meanings.

 

To refer to the value stored at the address (for use in normal

Calculations)

 

To allow storage of data at the address (for use in assignment

Statements)

 

Now we want to get a number from a pointer.

Integer myNum limit 7

RefToInteger myNumPtr success & myNum

Print "the value stored in myNum is", myNum

Print "the value, again, is", * myNumPtr

MyNum limit * myNumPtr + 1 // Pay attention to this line

 

* MyNumPtr refers to dereference, which is to read the actual meaning from a pointer's address.

In the preceding example, we know that:

RefToInteger myNumPtr is a pointer to integer.

MyNumptr is an address

* MyNumPtr is a value, and which contain address is 7

7 + 1 equals 8

Then * the value of myNumPtr remains unchanged, and the value of myNum is assigned again.

 

Another example:

Integer myNum limit 7

RefToInteger myNumPtr success & myNum

* MyNumPtr handle * myNumPtr + 1 // Pay attention to this line

Here:

MyNum is an integer with a value of 7.

MyNumptr is a pointer to a integer. // The value of myNumPtr is an address code with no actual meaning. // the address of myNum is copied to myNumptr.

Therefore, * the value of myNumPtr is 7.

And then * myNumPtr auto-increment, so * myNumPtr will be 8 at this time

However, the value of myNum is still 7.

 

Remember, when A operation B, A and B will always be the same type (unless there is A forced conversion format). So in general judgment, first, check whether the two sides of "=" and "=" are in the same format.

Assume that

1:

MyNumPtr = 1;

At this time, we know that myNumPtr is a pointer, whose value is an address. Generally, it is a string of numbers, and the value assignment symbol is an integer 1 on the right. Therefore, this is incorrect.

2:

* MyNumptr = & myNum

At this time, the left side of the equal sign is a value, a pointer refers to a value, and the right side is an address, so this is also incorrect.

3:

Int a = 'a ';

At this time, there is an integer a on the left of the value assignment. We need to assign a value to a, so only one integer can be assigned. The right of the equal sign is a char value, so this is incorrect.

 

In summary, when we encounter problems such as pointers, remember to pay attention to whether the left and right sides are in the same format.

 

 

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.