指针 (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