C ++ basic accumulation (2) explanation of the relationship between pointers and arrays, pointers and functions

Source: Internet
Author: User

Reprinted! Please indicate the source when reprint: http://blog.csdn.net/aa4790139/article/details/8144416

This article will detail these relationships...

First, let's figure out the priority of the operator:

Priority Operator Description Example Overloading Associativity
1 :: Scope resolution (C ++ proprietary) Class::age = 2; No From left to right
2 ++ Increasing suffix I ++  
-- Decrease suffix I --  
{} Combination {I ++; A * = I ;}  
() Function call or variable Initialization C_tor (int x, int y): _ x (x), _ y (y * 10 ){}  
[] Array access Array [4] = 2;  
. Access members as objects OBJ. Age = 34; No
-> Access members using pointers PTR-> age = 34;  
dynamic_cast Runtime check type conversion (C ++ proprietary) Y & Y = dynamic_cast <Y &> (X ); No
static_cast Unverified type conversion (C ++ proprietary) Y & Y = static_cast <Y &> (X ); No
reinterpret_cast Redefinition type conversion (C ++ proprietary) Int const * P = reinterpret_cast <int const *> (0x1234 ); No
const_cast Change the extraordinary property (C ++ proprietary) Int * q = const_cast <int *> (P ); No
typeid Obtain type information (C ++ proprietary) STD: type_info const & t = typeid (X ); No
3 ++ Increasing prefix ++ I   From right to left
-- Decrease prefix -- I  
+ Mona1 normal number Int I = + 1;  
- One dollar negative number Int I =-1;  
!
not
Non-logical
!Alternate spelling
If (! Done )...  
~
compl
Bitwise Inversion
~Alternate spelling
Flag1 = ~ Flag2;  
(type) Forced type conversion Int I = (INT) floatnum;  
* Reference Int DATA = * intptr;  
& Take the address of XX (refer) Int * intptr = & data;  
sizeof XX size Size_t S = sizeof (INT ); No
new Dynamic Memory Allocation (C ++ proprietary) Long * pvar = new long;  
new[] Dynamic Array Memory Allocation (C ++ proprietary) Long * array = new long [20];  
delete Dynamic memory release (C ++ proprietary) Delete pvar;  
delete[] Dynamic Array Memory release (C ++ proprietary) Delete [] array;  
4 .* Member object selection (C ++ private) OBJ. * Var = 24; No From left to right
->* Member pointer selection (C ++ proprietary) PTR-> * Var = 24;  
5 * Multiplication Int I = 2*4;  
/ Division Float F = 10.0/3.0;  
% Modulus (remainder) Int REM = 4% 3;  
6 + Addition Int I = 2 + 3;  
- Subtraction Int I = 5-1;  
7 << Shifts bits left Int flags = 33 <1;  
>> Shift bit right Int flags = 33> 1;  
8 < Less than link If (I <42 )...  
<= Less than or equal If (I <= 42 )...  
> Greater than link If (I> 42 )...  
>= Greater than or equal to the link If (I> = 42 )...  
9 ==
eq
Equal to link
==Alternate spelling
If (I = 42 )...  
!=
not_eq
Not equal to link
!=Alternate spelling
If (I! = 42 )...  
10 &
bitand
Bit and
&Alternate spelling
Flag1 = flag2 & 42;  
11 ^
xor
Bit XOR (exclusive or)
^Alternate spelling
Flag1 = flag2 ^ 42;  
12 |
bitor
Bit or (including or)
|Alternate spelling
Flag1 = flag2 | 42;  
13 &&
and
Logic and
&&Alternate spelling
If (conditiona & conditionb )...  
14 ||
or
Logic or
||Alternate spelling
If (conditiona | conditionb )...  
15 c?t:f Ternary conditional operation Int I = A> B? A: B; No From right to left
16 = Direct assignment Int A = B;  
+= And assign values A + = 3;  
-= Assignment by difference B-= 4;  
*= Assign values by multiplication A * = 5;  
/= Assign values by Division A/= 2;  
%= Returns the remainder value. A % = 3;  
<<= Shifts the value to the left using bits. Flags <= 2;  
>>= Shifts the value by bit to the right. Flags> = 2;  
&=
and_eq
Value by bit and
&=Alternate spelling
Flags & = new_flags;  
^=
xor_eq
Assign values by bit XOR
^=Alternate spelling
Flags ^ = new_flags;  
|=
or_eq
Value by bit or
|=Alternate spelling
Flags | = new_flags;  
17 throw Throw an exception Throw eclass ("message "); No
18 , Cyclic Evaluation For (I = 0, j = 0; I <10; I ++, J ++ )...   From left to right

Remember a rough idea. Otherwise, it would be too big to remember so many heads...

Operator priority: parentheses> Arithmetic Operators> bitwise operators> value assignment operators

Pointer: Int * P points to an int type pointer variable.

Pointer Array: Int * P [] an array composed of pointers (combined with P [] And then int *)

Array pointer: Int (* P) [] refers to a pointer to a one-dimensional array (first combined with (* P), then pointing to int []).

Pointer Functions: Int * P () returns an int type pointer function.

Function pointer: Int (* p) () pointer to a function entry

Two-dimensional pointer:Int ** P; pointer variable pointing to an int type pointer

 

Why do we need pointers? For example, why does an array exist?

For example:

Char s [3] [3] = {"A", "de", "H "};

Cout <sizeof (s) <Endl;

Char * P [] = {"A", "de", "H "};
 
// Cout <sizeof (p) <Endl; I don't know how to print the size of the memory occupied by the P pointer pointing to the address .... if a friend knows how to print a pointer pointing to the address to occupy the memory, please let me know? Thanks

Running result:

9

7

In fact, they all know the truth...

S [3] [5] The first character is not five characters long enough. It is an ending character, and one byte is not used. The same is true for the subsequent character... the array pointed to by P directly points to, no waste of money...

 

Adding char occupies one byte, so this array occupies

 

I think the following examples are quite good ~ Then, ask your answers and check if they are the same as the results below. In this way, your understanding and impressions can be deepened.

1. Int X [5]; int ** P = & X; do you think this is correct?

Answer: an error is returned because the type does not match. The P type is int **, And the & X type int (*) [5] is obtained because the X address is obtained, that is to say, this address is the address of the array, not just the pointer of the first element of the array (two-dimensional pointer), but the address of the entire array. The correct method is as follows: int (* P) [5] = & X;

2. How many bytes does int X [5]; int (* P3) [5]; how many bytes does P3 ++ move forward?

Answer: P3 points to an array, which is the entire array. Therefore, when P3 moves, it regards an array as a whole. So move 4*5 bytes (INT occupies 4 bytes)

3. Can int A [2] [3]; int ** P = & A; initialize it like this?

Answer: Certainly not. The type of... & A is int (*) [2] [3], and the type of P is int **;

1: int (* P) [2] [3] = &;

Correct syntax 2: int (* P) [3] =;

The second method is explained: A [0] And a [2] are also three pointers.

A [0] ---> A [0] [0], a [0] [1], a [0] [2]

A [1] ---> A [1] [0], a [1] [1], a [1] [2]

In fact, array A has two elements: a [0] And a [1]. Then, the value of a is the address of the first element, that is, & A [0]. What type is this? We know that if we regard a [0] as a whole, for example, if we use a to replace a [0], then a [0], A [1] is equivalent to a [0] [0], a [0] [1]. In this case, a is an array of the int type, and the & A type is actually the INT (* P) [3] type.

I hope you can read it carefully...

Thank you for reading my blog and hope you can see your footprints!
If something is wrong or incorrect,I also hope you can point it out!

References:

Http://zh.wikipedia.org/wiki/C%E5%92%8CC%2B%2B%E9%81%8B%E7% AE %97%E5%AD%90

Http://www.360doc.com/content/11/0506/22/6903212_114913991.shtml

 

 

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.