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 &)
<1> & get the address Operator
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 < }
<2> * 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 <* p1 <"\ n ";
Cout <* (p1 + 1) <"\ n ";
}

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 <* 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 ))
}

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 <* p1 <"\ n ";
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 < }
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

This article is from the "pure" Programmer Club "blog. For more information, contact the author!

Related Article

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.