美文网首页
c++指针与引用

c++指针与引用

作者: 85dc0dea73fd | 来源:发表于2019-02-22 08:12 被阅读0次

    title: c++之指针与引用
    tags:


    指针与引用


    函数结构


    image.png

    什么是指针

    指针就是一个变量,这个变量用来存储内存地址。
    换个说法,指针里面包含的值被解读为内存地址。
    即指针是指向内存单元的特殊变量。
    表示符号,0-9,A-F
    变量都需要先应用后使用,指针也不例外,需要先声明。通常将指针声明为指向特定的类型,如 int,这意味着指针包
    含的地址对应的内存单元存储了一个整数。也可将指针声明为指向一个内存块,这种指针被称为 void
    指针。
    声明如下:
    PointedType * PointerVariableName = NULL; // initializing value
    因此,声明 int 指针的代码如下:
    int *pointsToInt = NULL;

    使用引用运算符(&)获取变量的地址

    程序清单 8.1 获取 int 变量和 double 变量的地址

     #include <iostream> 
     using namespace std;  
     int main() 
      { 
      int age = 30; 
      const double Pi = 3.1416;  
     cout << "Integer age is located at: 0x" << &age << endl; 
     cout << "Double Pi is located at: 0x" << &Pi << endl;  
    return 0; 
     } 
    

    输出:
    Integer age is at: 0x0045FE00
    Double Pi is located at: 0x0045FDF8
    简而言之,& 用来取变量的地址。

    使用指针 存储 由地址运算符(&)获取的地址。

    Type VariableName = InitialValue;
    Type* Pointer = &Variable; 变量的类型与指针的类型相同

    int age = 30;
    int* pointsToInt = &age;
    注: 同一个 int 指针(pointsToInt)可指向任何 int 变量

    使用解除引用运算符(*)访问指向的数据

    即 如果point是一个指针,例如
    int age = 30 ;
    int* point = &age; 此时,point存储的是age的地址,即ox。。。。
    我们可以用 point 来取出point存储的地址里面的变量,即point= 30;

    猪猪猪猪:存储指针的内存量都是相同的

    使用 new 和 delete 动态地分配和释放内存

    使用 new 时,需要指定要为哪种数据类型分配内存:
    Type* Pointer = new Type; // request memory for one element
    需要为多个元素分配内存时,还可指定要为多少个元素分配内存:
    Type* Pointer = new Type[numElements]; // request memory for numElements
    因此,如果需要给整型分配内存,可使用如下语法:
    int* pointToAnInt = new int; // get a pointer to an integer
    int* pointToNums = new int[10]; // pointer to a block of 10 integers

    使用 new 分配的内存最终都需使用对应的 delete 进行释放:
    Type* Pointer = new Type; // allocate memory
    delete Pointer; // release memory allocated above
    这种规则也适用于为多个元素分配的内存:
    Type* Pointer = new Type[numElements]; // allocate a block
    delete[] Pointer; // release block allocated above

    对于使用 new[…]分配的内存块,需要使用 delete[]来释放;对于使用 new 为单个元素分
    配的内存,需要使用 delete 来释放。

    *例程使用解除引用运算符()访问使用 new 分配的内存,并使用 delete 释放它

     #include <iostream> 
     using namespace std;  
     int main() 
      {  
      int* pointsToAnAge = new int; 
     cout << "Enter your dog’s age: "; 
     cin >> *pointsToAnAge;   
    cout << "Age " << *pointsToAnAge << " is stored at 0x" << hex << pointsToAnAge << endl;  
     delete pointsToAnAge; 
     
    17: return 0; 
    18: } 
    输出:
    Enter your dog's age: 9 
    Age 9 is stored at 0x00338120 
    

    将关键字 const 用于指针

    const 指针有如下三种。
    • 指针包含的地址是常量,不能修改,但可修改指针指向的数据:
    int daysInMonth = 30;
    int* const pDaysInMonth = &daysInMonth;
    pDaysInMonth = 31; // OK! Data pointed to can be changed
    int daysInLunarMonth = 28;
    pDaysInMonth = &daysInLunarMonth; // Not OK! Cannot change address!
    • 指针指向的数据为常量,不能修改,但可以修改指针包含的地址,即指针可以指向其他地方:
    int hoursInDay = 24;
    const int
    pointsToInt = &hoursInDay;
    int monthsInYear = 12;
    pointsToInt = &monthsInYear; // OK!
    pointsToInt = 13; // Not OK! Cannot change data being pointed to
    int
    newPointer = pointsToInt; // Not OK! Cannot assign const to non-const
    • 指针包含的地址以及它指向的值都是常量,不能修改(这种组合最严格):
    int hoursInDay = 24;
    const int* const pHoursInDay = &hoursInDay;
    *pHoursInDay = 25; // Not OK! Cannot change data being pointed to
    int daysInMonth = 30;
    pHoursInDay = &daysInMonth; // Not OK! Cannot change address
    将指针传递给函数时,这些形式的 const 很有用。


    引用是什么

    引用是变量的别名。声明引用时,需要将其初始化为一个变量,因此引用只是另一种访问相应变
    量存储的数据的方式。
    要声明引用,可使用引用运算符(&),如下面的语句所示:
    VarType original = Value;
    VarType& ReferenceVariable = original;

    **程序 引用是相应变量的别名**
    
     #include <iostream> 
     using namespace std; 
     int main() 
     { 
     int original = 30; 
    cout << "original = " << original << endl; 
     cout << "original is at address: " << hex << &original << endl; 
     int& ref1 = original; 
    cout << "ref1 is at address: " << hex << &ref1 << endl; 
    int& ref2 = ref1; 
     cout << "ref2 is at address: " << hex << &ref2 << endl; 
     cout << "Therefore, ref2 = " << dec << ref2 << endl; 
    return 0; 
     } 
    输出:
    original = 30 
    original is at address: 0099F764 
    ref1 is at address: 0099F764 
    ref2 is at address: 0099F764 
    Therefore, ref2 = 30 
    
    

    文章依据21天学通C++第八版,纯属小白自学!!!!

    相关文章

      网友评论

          本文标题:c++指针与引用

          本文链接:https://www.haomeiwen.com/subject/wklkyqtx.html