C + + pointers Learning notes

Source: Internet
Author: User

This article refer to http://www.prglab.com/cms/pages/c-tutorial/advanced-data/pointers.php

1. Variables that store other variable addresses (such as address in the example Below) are called pointers (pointer).

2. Address operator/dereference operator

The symbol ampersand sign (&), which is added before the variable name identifier, expresses the meaning "... Address of ... ) ", so called the address Operator.

For example,:address=&value1;//address represents the address of value1

Assign the address of the variable value1 to the variable, because when the variable name value1 is preceded by the ampersand (&) symbol, we will not refer to the Variable's contents, but to its in-memory address.

3. Reference operator (*)

When using a pointer, we can store the value stored by the variable pointed to by the pointer by adding an asterisk asterisk (*) to the pointer before it can be translated to "value pointed by".

e.g. value=*address; Value is the value pointed to by address

Address or inverse reference operator operator of address or dereference (&)

It is used as a variable prefix and can be translated as "... ("address of"), therefore: &variable1 can be read as the address of variable1 ("addresses of variable1").

Reference operator operator of reference (*)

It represents the content to be taken by the address that the expression Represents. It can be translated as "... The value pointed to "(" value pointed by ").

* Mypointer can be read as "mypointer pointing to the value".

4. Declaration and use of pointers

(1). Declaration of the pointer

Type * pointer_name;

Where type refers to the type of the variable pointed to by the pointer, Pointer_name refers to the name of the pointer variable;

for example: int * number;
char * character;
FLOAT * greatnumber;

(2) use of pointers

#include <iostream.h>intmain () {intValue1 =5, value2 = the;int*p1, *P2;P1= &value1;//P1 = Address of value1P2 = &value2;//P2 = Address of value2*P1 =Ten;//value pointed by P1 = ten*P2 = *p1;//value pointed by P2 = value pointed by P1P1 = p2;//P1 = p2 (value of pointer copied)*P1 = -;//value pointed by P1 =cout <<"value1=="<< value1 <<"/value2=="<<value2;return 0;}

(3) Pointer and array

The concept of arrays is very closely related to Pointers. The identifier of an array is equivalent to the address of its first element, as if it were the same as the address of the first element it points to.

For example:

int numbers[]; int *p;

The following assignment is Legal:

p=number;

Here the pointer p and numbers are equivalent, they have the same properties, the only difference is that we can assign other values to the pointer p, and numbers always points to the first of the 20 integer groups Defined. so, P is a normal pointer variable , and unlike this, numbers is a pointer constant (constant pointer), and the array name is indeed a pointer constant. So while the previous assignment expression is legal, the following is Not:

numbers = p;

Because number is an array (pointer constant), the constant identifier cannot be assigned another Value.

//Array (constant pointer) points to a contiguous address space in memory#include <iostream.h>intmain () {intnumbers[5];int*p;p= numbers;//assigns the first address of the array numbers to the pointer variable p*p =Ten;//the first element of the array is assigned a value of ten through the pointer pp++;//The amount of memory that the pointer address value plus an integer variable occupies*p = -;//Assigning a value to the second element of an arrayp = &numbers[2]; *p = -;//assigns a value to the third element of an arrayp = numbers +3; *p = +;//assigns a value to the third element of an arrayp = numbers; * (p+4) = -;  for(intn=0; n<5; n++)//output array of all element values cout<< numbers[n] <<", "; return 0; }

(4) mathematical operation of the pointer

The pointer has only addition and subtraction operations, and other operations are meaningless in the world of Pointers. But the specific operation of the addition and subtraction of pointers differs depending on the size of the type of data it points to. We know that different data types are not the same storage space that is consumed in memory. For example, the character Char occupies 1 bytes (1 byte), The short integer takes 2 bytes, and the long integer length occupies 4 bytes.

Let's say we have 3 pointers:

Char *mychar;  Short *myshort; Long *mylong;

also, assume that they point to memory at 1000, 2000, and 3000 respectively. When they do the Self-add operation, the effect is as Follows:

mychar++;             // the address of the MyChar becomes 1001myshort++;           // the address of the MyShort becomes 2002mylong++;            // MyLong address changed to 3004

It is important to note that the increment (+ +) and decrement (-) operations transmitting reference operators reference operator (*) have higher precedence

*p++; //      equivalent to (* (p++)), it is done by adding P (the address It points to, not the value it stores)*p++ = *q++; // the two increment operation (+ +) is performed after the entire expression is evaluated, not before, so the value of *q is first given to *p, then Q and P are incremented by 1, // It is equivalent to:*p = *q;p+ +; q+ +;

C + + pointers Learning notes

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.