当前位置 : 主页 > 编程语言 > c++ >

c++ 指针(二)

来源:互联网 收集:自由互联 发布时间:2021-06-23
指针 (4)指针数组 #include iostream using namespace std; const int MAX = 3 ; int main (){ int var [MAX] = { 10 , 100 , 200 }; for ( int i = 0 ; i MAX; i++ ) { cout " Value of var[ " i " ] = " ; cout var [i] endl; } return 0 ;} Valu

指针

(4)指针数组

#include <iostream>
 
using namespace std;
const int MAX = 3;
 
int main ()
{
   int  var[MAX] = {10, 100, 200};
 
   for (int i = 0; i < MAX; i++)
   {
      cout << "Value of var[" << i << "] = ";
      cout << var[i] << endl;
   }
   return 0;
}
Value of var[0] = 10
Value of var[1] = 100
Value of var[2] = 200

指向整数的指针数组的声明:

int *ptr[MAX];

把 ptr 声明为一个数组,由 MAX 个整数指针组成。因此,ptr 中的每个元素,都是一个指向 int 值的指针

#include <iostream>
 
using namespace std;
const int MAX = 3;
 
int main ()
{
   int  var[MAX] = {10, 100, 200};
   int *ptr[MAX];
 
   for (int i = 0; i < MAX; i++)
   {
      ptr[i] = &var[i]; // 赋值为整数的地址
   }
   for (int i = 0; i < MAX; i++)
   {
      cout << "Value of var[" << i << "] = ";
      cout << *ptr[i] << endl;
   }
   return 0;
}
Value of var[0] = 10
Value of var[1] = 100
Value of var[2] = 200

也可以用一个指向字符的指针数组来存储一个字符串列表:

#include <iostream>
 
using namespace std;
const int MAX = 4;
 
int main ()
{
 const char *names[MAX] = {
                   "Zara Ali",
                   "Hina Ali",
                   "Nuha Ali",
                   "Sara Ali",
   };
 
   for (int i = 0; i < MAX; i++)
   {
      cout << "Value of names[" << i << "] = ";
      cout << names[i] << endl;
   }
   return 0;
}
Value of names[0] = Zara Ali
Value of names[1] = Hina Ali
Value of names[2] = Nuha Ali
Value of names[3] = Sara Ali

char *names[MAX] 是指针数组, 它的本质是存储指针的数组, 既存储 char 类型的指针的数组, 数组内的每个元素都是一个指针指向一个存储 char 类型的地址: 

#include <iostream>
using namespace std;

const int MAX = 4;

int main(int argc, const char * argv[]) {

    const char *names[MAX] = {
        "Zara Ali",
        "Hina Ali",
        "Nuha Ali",
        "Sara Ali",
    };

    for (int i = 0; i < MAX; i++) {
        cout << " --- names[i]              = " << names[i] << endl;
        cout << " --- *names[i]             = " << *names[i] << endl;
        cout << endl;
        cout << " --- (*names[i] + 1)       = " << (*names[i] + 1) << endl;
        cout << " --- (char)(*names[i] + 1) = " << (char)(*names[i] + 1) << endl;
        cout << " ------------------------------------ " << endl << endl << endl << endl;
    }
    return 0;
}

 

 

 

cout<<*names[i]<<endl; 输出结果为对应得指针数组的第一个字符,因为 *name 代表指向数组的第一个地址的值

#include <iostream>
using namespace std;

void pointerArray();
void pointerArray4Char();
const int MAX = 3;

int main(void){
    // pointerArray();
    pointerArray4Char();
    return 0;
}

void pointerArray(){
    int var[MAX] = {20,30,40};
    int *ptr[MAX];
    for(int i = 0; i < MAX; i++){
        ptr[i] = &var[i];//赋值为整数的地址
    }
    for(int i = 0; i < MAX; i++){
        cout << "Value of var[" << i << "] = ";
        cout << *ptr[i] <<endl;
    }
}

/**
 * 用一个指向字符的指针数组来存储一个字符串列表
 * Value of names[0] = sun;
 */
void pointerArray4Char(){
    const char *names[MAX] = {
            "sun","bin","sunbin"
    };
    for(int i = 0;i < MAX;i++){
        cout <<"Value of names[" << i << "] = ";//输出字符串的值
        cout << names[i] << endl;
        cout <<"Value of *names[" << i << "] = ";//输出指针所指向字符串首地址的值
        cout << *names[i] << endl;
        cout <<"Value of *names[" << i << "]+1 = ";//输出ascii码值
        cout << *names[i]+1 << endl;
        cout <<"Value of *(names[" << i << "]+1) = ";//输出指针所指向字符串首地址上一位的值
        cout << *(names[i]+1) << endl;
    }
}

 

 

 

 

 

 

 char *names[MAX] 这种字符型的指针数组是存储指针的数组,但是在理解字符型指针数组的时候,可以将它理解为一个二维数组,如 const char *names[4] = {"Zara Ali","Hina Ali","Nuha Ali","Sara Ali",} 可以理解为一个 4 行 8 列的数组,可以用 cout << *(names[i] + j)<< endl 取出数组中的每个元素。

#include <iostream>

using namespace std;

const int MAX = 4;
int main ()
{
    const char *names[MAX] = {
        "Zara Ali",
        "Hina Ali",
        "Nuha Ali",
        "Sara Ali",
    };

    for (int i = 0; i < MAX; i++)
        for (int j = 0; j < 8; j++)
        {
            cout << "Value of names[" << i << "] = ";
            cout << *(names[i] + j)<< endl;
        }
    return 0;
}

 

 结果:

Value of names[0] = Z Value of names[0] = a Value of names[0] = r Value of names[0] = a Value of names[0] = Value of names[0] = A Value of names[0] = l Value of names[0] = i Value of names[1] = H Value of names[1] = i Value of names[1] = n Value of names[1] = a Value of names[1] = Value of names[1] = A Value of names[1] = l Value of names[1] = i Value of names[2] = N Value of names[2] = u Value of names[2] = h Value of names[2] = a Value of names[2] = Value of names[2] = A Value of names[2] = l Value of names[2] = i Value of names[3] = S Value of names[3] = a Value of names[3] = r Value of names[3] = a Value of names[3] = Value of names[3] = A Value of names[3] = l Value of names[3] = i

 

 

 

(5)指向指针的指针

指向指针的指针是一种多级间接寻址的形式,或者说是一个指针链。通常,一个指针包含一个变量的地址。当我们定义一个指向指针的指针时,第一个指针包含了第二个指针的地址,第二个指针指向包含实际值的位置。

一个指向指针的指针变量必须如下声明,即在变量名前放置两个星号。例如,下面声明了一个指向 int 类型指针的指针:

int **var;

 

当一个目标值被一个指针间接指向到另一个指针时,访问这个值需要使用两个星号运算符

#include <iostream>
 
using namespace std;
 
int main ()
{
    int  var;
    int  *ptr;
    int  **pptr;
 
    var = 3000;
 
    // 获取 var 的地址
    ptr = &var;
 
    // 使用运算符 & 获取 ptr 的地址
    pptr = &ptr;
 
    // 使用 pptr 获取值
    cout << "var 值为 :" << var << endl;
    cout << "*ptr 值为:" << *ptr << endl;
    cout << "**pptr 值为:" << **pptr << endl;
 
    return 0;
}

 

结果:

var 值为 :3000
*ptr 值为:3000
**pptr 值为:3000

 

 

 

网友评论