指向字符的指针数组来存储一个字符串列表

xiaoxiao2021-02-28  9

代码1:

#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

而如果变成: cout << "*var[" << i << "]="; cout << *var[i] << endl; 输出: *var[0]=Z *var[1]=H *var[2]=N *var[3]=S 是首字母哦

转载请注明原文地址: https://www.6miu.com/read-200001.html

最新回复(0)