C ++ learning (3) - Pointer

pointer

1. pointer occupies memory space

In the 32-bit operating system, 4 bytes, 8 bytes at 64

2. null pointer with a pointer field

  • Null pointer: a pointer variable to point to memory spaces numbered 0

    • Uses: initialize the pointer variable

    • Note: The amount of memory is not a null pointer access

      The memory number between 0 and 255 is occupied by the system, and therefore not accessible

    int *p = NULL;
    cout << *p << endl;   //访问会报错
  • Wild pointer: pointer variable points to an illegal memory space

    int *p = (int *)0x1100;    
  • Null pointer and the pointer is not a wild space of our application, so do not visit

3. const pointer modification

const pointer modified in three ways:

  1. const modifier pointer - pointer constant
  2. const modified constant - pointer constant
  3. const pointer both modified, and modified constants
  • const modifier pointer - pointer constant

    int a = 10;
    const int *p = &a;

    Features: pointers can be modified, but the value of the pointer can not be modified

    *p = 20;  //×
    int b = 20;
    p = &b;   //√
  • const modified constant - pointer constant

    int a = 10;
    int * const p = &a;

    Features: pointer to not change, pointer values can be changed

    *p = 20;  //√
    int b = 20;
    p = &b;   //×
  • const pointer both modified, and modified constants

    int a = 10;
    const int * const p = &a;

    Features: pointers and pointer values may be modified not

    *p = 20;  //×
    int b = 20;
    p = &b;   //×

4. pointers and arrays

Data access the pointer array

int arr[3] = {1,2,3};
int *p = arr;
/* 此时*p==arr[0] */
//数组在内存中的空间是连续的,可以通过指针自加访问下一元素
p++;  
/* 此时*p==arr[1] */

Guess you like

Origin www.cnblogs.com/maeryouyou/p/11941420.html