C++之字符指针数组与整数指针数组

#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 << " --- 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;
}

结果:

 --- names[i]              = Zara Ali
 --- *names[i]             = Z

 --- (*names[i] + 1)       = 91
 --- (char)(*names[i] + 1) = [
 ------------------------------------ 



 --- names[i]              = Hina Ali
 --- *names[i]             = H

 --- (*names[i] + 1)       = 73
 --- (char)(*names[i] + 1) = I
 ------------------------------------ 



 --- names[i]              = Nuha Ali
 --- *names[i]             = N

 --- (*names[i] + 1)       = 79
 --- (char)(*names[i] + 1) = O
 ------------------------------------ 



 --- names[i]              = Sara Ali
 --- *names[i]             = S

 --- (*names[i] + 1)       = 84
 --- (char)(*names[i] + 1) = T
 ------------------------------------ 

注意name[i]与*name[i]的区别

names可以理解为一个 4 行 8 列的数组,可以用 cout << *(names[i] + j)<< endl 取出数组中的每个元素

 


#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;
	   
      cout << "---------------------------" << endl;
	   
      cout << *ptr[i] << endl;
   }
   return 0;
}

结果:

Value of var[0] = 0x7ffd763ebc3c
---------------------------
10
Value of var[1] = 0x7ffd763ebc40
---------------------------
100
Value of var[2] = 0x7ffd763ebc44
---------------------------
200

注意ptr[i]与*ptr[i]的区别

 

参考C++菜鸟驿站指针数组

C++指针数组

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值