Array-pointer-Reference

Source: Internet
Author: User
I. pointer Concept

The pointer is the address in the memory.

1. Syntax: type * variable name
The type here defines the variable type pointed to by this pointer.

2. pointer operators (* and &)
& Get the address character
For example:
Int counta = 100;
Int * mm;
Mm = & counta;
Assume that the counta address is 2000, which is waiting for M = 2000.

# Include
Void main ()
{
Int counta = 100;
Int * mm;
Mm = & counta;
Cout
}
* Return the value of this address, which is opposite.

# Include
Void main ()
{
Int P, counta = 100;
Int * mm;
Mm = & counta;
P = * mm;
Cout
}

3. pointer assignment

# Include
Void main ()
{
Int X;
Int * P1, * P2;
P1 = & X;
P2 = p1;
Cout
}

Result: 0x0012ff7c.

4. pointer operation
The ++ and -- of the pointer move the pointer address to the number of digits of the variable type.

Char 8
Int 16
Long 32
Float 32
Double 64

5. pointers and Arrays

Array declaration: type variable name [length]

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

# Include
Void main ()
{
Int X [3] = {1, 2, 3 };
Int * P1;
P1 = X;
Cout
}

A. equivalence relationship:

For example, char C [10];
C and & C [0] are equivalent.

For example, char C [2] [3];
C and & C [0] [0] are equivalent.
* (C + 12) and & C [1] [2] are equivalent.

B. Relationship between arrays and pointers

1> pointer to one-dimensional array

# Include
Void main ()
{
Int X [2] = {1, 2 };
Int * P1;
P1 = X;
Cout
Cout
}

2> two-dimensional array pointer

# Include
Void main ()
{
Int
X [2] [3] = {1, 2, 4, 5, 6 };
// Int X [2] = {1, 2 };
Int * P1;
P1 = & X [0] [0]; // For a two-dimensional array, the pointer cannot be assigned with the value "p1 = x". It can only be "p1 = x [2]".
Cout
Cout
Cout
Cout
Cout
Cout
Cout
Cout
Cout
Cout
Cout
Cout
// Rule: * (P1 + (1*3) + 2 ))
}

Think: if the pointer is an address, how can I get the variable of an address.

2. Compare references with pointers

A reference is the alias of a variable.

# Include
Void main ()
{
Int x P1, A = 100;
Int & Y1 = A; // values must be assigned directly.
P1 = &;
Y1 =;
Cout
Cout
}

Pointers and references can achieve the same effect.

# Include
Void main ()
{
Void funca (Int & Vala );
Void funcp (int * valp );
Int A = 100, B = 100;
Int & Y1 = A; // values must be assigned only once.
Funca ();
Funcp (& B );
Cout
Cout
}
Void funca (Int & Vala)
{
Vala = 200;
}
Void funcp (int * valp)
{
* Valp = 500;
}

3. Use and to avoid obfuscation of these symbols.

1. * functions:

1> multiplication number
2> pointer definition symbol
3> return the value of an address.

2. & role:

1> "and" In bitwise operations"
2> get the address character
3> reference

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.