Pointers and references (II.)

Source: Internet
Author: User
Tags constant

16.4 assigned pointers and references.

Just like you see Using the syntax of the "addressing" operator, a pointer is assigned to the return value of the "addressing" operator. Because the return value of the "addressing" operator is a pointer, everything

have been considered, your code should be compiled. For an assignment to a pointer, it must get an address in memory or the compiler will give you a hint of error.

int x;
int* px = &x;

The preceding code illustrates the variable x declared as the int type, a variable declared as a pointer, and the address of the assigned memory x. Pointer px actually "points" to x by the address stored in memory x. Remember that when you declare a pointer, you need a pointer of the same type to act as a variable or constant in your address.

Now you start to understand the difference between pointers and references. Assigning a pointer to an address in memory, you have to use the "addressing" operator to return the address of the pointer variable in memory.

A references, however, does not need to be assigned to an in-memory address using the "addressing" operator. Assign an address to a reference, you only need to use a variable to act as a rvalue value on the line.int x;
int& rx=x;

The above code shows that the variable x of type int is declared, and then the reference Rx is also declared, and is assigned to "act as" x. Note that regardless of how the address of x is stored in the RX, or "referenced" via Rx, you do not have to use other operators, just variables. When you encounter situations where you have to declare the same type of reference as a variable or constant, you must also follow such a method to act as a pointer.

It's possible that you want to know what a pointer can show, like this:#include <iostream.h>
int main()
{
int someNumber = 12345;
int* ptrSomeNumber = &someNumber;
cout << "someNumber=" << &someNumber <<endl;
cout << "ptrSomeNumber=" << prtSomeNumber <<endl;
return 0;
}

If you compile and run this code, you should get the variable somenumber output 12345,ptrsomenumber will output a hexadecimal number (in-memory address is implemented in hexadecimal). Now, if you want to output the value that Prtsomenumber refers to, you should use this code:

#include <iostream.h>
int main()
{
int someNumber = 12345;
int* ptrSomeNumber = &someNumber;
cout << "someNumber=" << &someNumber <<endl;
cout << "ptrSomeNumber points to " << *prtSomeNumber <<endl;
return 0;
}

So in principle, when you want to use, modify, or manipulate the value that pointer x refers to, you can use *X to act as this value or variable.

Here's a quick list of pointers and referenceds can do:

You can assign a pointer to "point" to an address in memory.

You can assign a value reference to "act as" a variable or constant.

You can copy pointer values to other pointers.

You can individually modify the values that the pointer or references refers to or acts as stored in memory.

You can increment or decrement the address that the pointer refers to.

You can pass pointers and references to functions (more information is found in "passing through reference").

...... Cond.

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.