Array - pointer - reference

xiaoxiao2021-03-06  79

First, the concept of pointer

The pointer is the address in memory.

1, grammar: Type * variable name

The type here is defined the type of variable points to this pointer.

2, the operator of the pointer (* and &)

<1> & Take Address

E.g:

INT counta = 100;

INT * mm;

MM = & counta;

Suppose the address of Counta is 2000, this is M = 2000.

#include

void main ()

{

INT counta = 100;

INT * mm;

MM = & counta;

Cout <

<< "/ n";

}

<2> * Returns the value of this address, opposite to & just.

#include

void main ()

{

INT P, counta = 100;

INT * mm;

MM = & counta;

P = * mm;

Cout <

<< "/ n";} 3, the assignment of pointer #include

void main ()

{

INT X;

INT * P1, * P2;

P1 = & x;

P2 = P1;

Cout <

<< "/ n";

}

Results: 0x0012FF7C

4, pointer operation

Pointer and - is the number of variable type digits to move the address of the pointer.

Char 8

INT 16

Long 32

Float 32

Double 64

5, pointer and array

Array declaration: Type variable name [length]

The "one-dimensional" array name without the subscript is a pointer to the first element of the array.

#include

void main ()

{

INT x [3] = {1, 2, 3};

INT * P1;

P1 = x;

Cout <

<< "/ n";

}

a, equivalent relationship:

Such as: CHAR C [10];

C and & C [0] are equivalent.

Another example: char C [2] [3];

C and & C [0] [0] are equivalent.

* (C 12) and & C [1] [2] are equivalent.

b, array and pointer relationship

1> Pointer in one-dimensional array

#include

void main ()

{

INT x [2] = {1, 2};

INT * P1;

P1 = x;

Cout << * p1 << "/ n";

COUT << * (p1 1) << "/ n";

}

2> 2D array pointer

#include

void main ()

{

int

x [2] [3] = {1, 2, 3, 4, 5, 6};

// int x [2] = {1, 2};

INT * P1;

P1 = & x [0] [0]; // The two-dimensional array gives the pointer to assign a value with "P1 = X", can only be "p1 = x [2]"

Cout <

<< "/ n";

Cout <

<< "/ n";

Cout <

<< "/ n";

Cout <

<< "/ n";

Cout <

<< "/ n";

Cout <

<< "/ n";

Cout << * p1 << "/ n";

COUT << * (p1 1) << "/ n";

COUT << * (P1 2) << "/ n"; cout << * (p1 3) << "/ n";

COUT << * (P1 4) << "/ n";

COUT << * (P1 5) << "/ n";

// rule: * (p1 ((1 * 3) 2))))

}

Thinking: The pointer is the address, then what method is used if you want to get a variable of an address.

Second, compare references and pointers

Quote is the alias of the variable.

#include

void main ()

{

INT * P1, A = 100;

INT & Y1 = a; / / must be directly assigned

P1 = & a;

Y1 = a;

Cout << * p1 << "/ n";

Cout <

<< "/ n";

}

The pointer and reference can achieve the same effect.

#include

void main ()

{

Void Funca (INT & VALA);

Void Funcp (INT * VALP);

INT A = 100, b = 100;

INT & Y1 = a; / / must be directly assigned, and can only be assigned once

FUNCA (A);

Funcp (& B);

Cout <

<< "/ n"; cout <<< "/ n";} void funca {var = 200;} void funcp (int * valp) {* VALP = 500;} 3, * and & Uses, avoid these symbols in use, the effects of confusion 1, *: 1> Multiplier 2> Pointer definition symbol 3> Return to a value of an address 2, & effect: 1> "and" 2 " Take Address 1> Reference

转载请注明原文地址:https://www.9cbs.com/read-94155.html

New Post(0)