#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++菜鸟驿站指针数组