C ++ base (8) - the pointer concept, definition and calculation

Access mode memory space

  • By accessing the variable name;
  • Accessed by address.

The concept pointer

  • Pointer: memory address, indirect access to the memory unit;
  • Pointer variables: a variable for storing the address.

Defined pointer variable

//例 
static int i;
static int*ptr = &i;    //ptr为指向int变量的指针
*ptr = 3;    //指针变量的使用,i赋值为3

Associated with the address of the operation - "*" and "&"

  • Pointer operators: *
  • Address Operator: &

Initialize pointer variable

  • Syntax: * storage type data type name of the pointer = initial address;

    例:int *pa = &a;

  • Precautions:

    When a variable address as the initial value, the variable must be initialized before the pointer has been declared , and the pointer variable type should correspond to the type ;

    You can have a valid pointer value to initialize a pointer to another variable;

    Do not use an internal non-static variable to initialize static pointer.

Pointer variable assignment operator

  • Syntax: pointer address name =

  • Pointer type data type "address" is stored must match;

  • Assigned to the pointer variable values must be the address constant or variable , not an ordinary integer;

    For example, the address calculation to obtain the start address & variables and objects have been defined, the return address dynamic memory allocation success.

    An exception may be an integer from 0 to the pointer indicates a null pointer.

  • Allow definitions or declarations pointer to void pointer type . The address pointer may be given any type of object. Example void * genneral;

Null pointer nullptr

C ++ 11 nullptr keyword use, is more accurate expression, type the security of a null pointer.

Pointer const pointer constant

You can not change the value of the constant pointer referents, but the pointer itself may be changed, additional objects can point.

Pointer types of constants

If the statement pointer constant, the pointer value itself can not be changed.

//指针的定义、赋值和使用
#include<iostream>
using namespace std;
int main(){
    int i;
    int *ptr = &i;  //取i的地址赋给指针ptr
    i = 10;
    cout << "i = " << i << endl;
    cout << "*ptr = " << *ptr << endl;  //输出int型指针ptr所指地址的内容
    return 0;
}
//运行结果:
i = 10
*ptr = 10
    
//void类型指针的使用
#include<iostream>
using namespace std;
int main(){
    //!void voidObject; 错,不能声明void类型的变量
    void *pv;  //对,可以声明void类型的指针
    int i = 5;
    pv = &i;  //void类型指针指向整型变量
    int *pint = static_cast<int*>(pv);  //void指针转换为int指针
    cout << "*pint = " << *pint << endl;
    return 0;
}
//运行结果:
*pint = 5
    
//指向常量的指针
int a;
const int *p1 = &a;  //p1是指向常量的指针
int b;
p1 = &b;  //正确,p1本身的值可以改变
*p1 = 1;  //错误,不能通过p1改变所指的对象

//指针类型的常量
int a;
int *const p2 = &a;
p2 = &b;  //错误,p2是指针常量,值不能改变

Pointer arithmetic

Including subtraction pointer integer; ++ pointer, - calculation.

  • Pointer p plus or minus n

    Significance is the start position pointer points to the current position of the front or rear of the n-th data.

  • ++ pointer, - calculation

    Meaning a point before the next one or complete data starting.

  • The result of the operation depends on the data type of pointer always points to the start position of a data integrity.

  • When the pointer to the same type of data stored contiguously, integer addition and subtraction pointer increment and decrement makes sense.

Pointer type of relational operators

  • Point of the same type may be various relationships between operation data pointer;
  • Pointers point to different data types, and the relationship between the pointer and the general operation integer variable is meaningless;
  • Pointers may be equal or not equal and the relationship between arithmetic zero. E.g. p == 0 or p! = 0.
Published 76 original articles · won praise 30 · views 5854

Guess you like

Origin blog.csdn.net/weixin_45926367/article/details/104390123