Computing and storage pointer

Pointer calculation:

Resolving reference symbol (* &: can be canceled)

  * A number of bytes to take the contents

Subtraction between two pointers: the unit number of the phase difference between the two addresses;

  1 unit = sizeof (address pointed)

Then the concept of the previous chapter, said pointer, let's look at some examples

 

 int n[5] = { 5, 3, 1, 4, 2 };

int * p = n * P1;

p1 = &n[3];

int** pp = &p;

cout << (int*)(p1 - n) << endl; //00000003

  p1 point n [3] addresses, representative of n n [0] to address

  Therefore p1 - n = & n [3] - & n [0] = 12 / sizeof (int *) = 3;

  There convert int * type, so the final result is 00000003

cout << n[2]+++**pp<<endl; //6

  Rear jerk, to calculate n [2] + ** pp; n [2] is equal to 1, ** p points p, p and point n [0];

  Therefore n [2] + ** pp = n [2] + n [0] = 5 + 1 = 6; and n [2] ++, n [2] = 2;

cout << (*p1 == *p + 1) << endl; //0

  p1 point n [3], so * p1 = n [3] = 4, p points to n [0], so * p = n [0] = 5, 6 is equal to 1 plus

  So the result is 0

cout << (&p == &p1) << endl; //0

  p1 point n [3] addresses, n-point P [0] of the address, are not equal, the result is 0

cout << (int)p1 - (int)&n[0] << endl;

  p1 point n [3] of the address, (int) p1 - (int) & n [0] = (int) & n [3] - (int) & n [0] = n [3] to the n [0] distance , equal to 12

cout << *p + 1 << endl;//6

  point p n [0] address, the * p + 1 = n [0] +1 = 6;

cout << p - n << endl;//0

  point p n [0] of the address, but also point to the address n-n [0], so that pn = 0;

cout << *p - (*p1)++ << endl;//1

  point p n [0] address, p1 point n [3] of the address, the home ++, so * p- * p1 = n [0] - n [3] = 1; p1 points to the n [4];

cout << **pp << endl;//5

  pp point p, p and point n [0], so ** pp = n [0] = 5

cout << *p1***pp << endl;//25

  pp point p, p and point n [0], so ** pp = n [0] = 5, p1 points to the n [4] = 5, so the result is 25;

 

2

 int n[5] = { 1, 2, 3, 4, 5 };
 int* pn[4] = { n, &n[2], n, &n[0] };

 int ** ppn = pn; // ppn point pn [0]

 cout << **ppn + 1 << endl;//2

  ppn point pn [0], ** ppn = n [0] = 1; 2 result;

cout << ppn - &pn[2] << endl;//-2

  ppn - &pn[2] = &pn[0] - &pn[2] = -2;

cout << pn[2] - n << endl;//0

  pn [2] points to address n, n, n also points to the address, so the result is 0;

cout << *pn[2] + 2 << endl;//3

  pn [2] = & n [0], so * pn [2] + 2 = 1+ 2 = 3;

cout << *pn - n << endl;

  also points to address n pn, so the result is 0;

cout << (short*)pn - (short*)(&pn[3]) << endl;

  pn point pn [0] of the address, (short *) pn - (short *) (& pn [3]) = (short *) (& pn [0]) - (short *) (& pn [3]) = - 12; divided by sizeof (short *) = -12/2 = -6;

cout << (short*)((char*)pn[1] - (char*)&n[3]) << endl;

  pn[1] = &n[2] ;(short*)((char*)pn[1] - (char*)&n[3]) = (short*)((char*)&n[2] - (char*)&n[3]) = (short*)-4(转16进制) = fffffffc

char* pch = "hello world!";

  cout << sizeof(pch) << endl;//4

  Representative pch "hello world!", the length is generally any type pointer 4

cout << strlen(pch) << endl;//12

  "Hello world!" There are 12 characters

cout << sizeof(*pch) << endl;//1

  * Pch points to the first element of h; char length type 1

cout << *pch + 1<< endl;

  * Pch points to the first element of h; * pch + 1 = 'h' +1;

cout << pch << endl;//"hello world!"

  Print "hello world!"

cout << (* pch) ++ << endl; // error: Constants can not ++

  cout << *&pch << endl;//pch//"hello world!"

  Print "hello world!"

cout << (char*)(pch + 1) - pch << endl;//1

  pch itself is a char *, not changed

 

3

char* pch[3] = { "monday", "apple", "hello" };
 char** ppch = pch;

cout << *ppch << endl;

  PPCH point pch [0], so ppch = pch [0] = monday;

 cout << **ppch + 1<< endl;

  PPCH point pch [0], so ** ppch = 'm' + 1 = 78;

cout << (int*)(pch - &pch[1]) << endl;

  pch point pch [0] (int *) (pch - & pch [1]) = (int *) (& pch [0] - & pch [1]) = -1 (rotation FFFFFFFF in hexadecimal)

cout << pch[2] << endl;

  Print hello

cout << pch[2] - *ppch << endl;

  pch[2]  - *ppch = &'h' - &'m' =16;

Guess you like

Origin www.cnblogs.com/1448560633yang/p/11280005.html
Recommended