C++ 如何 打印字符串数组char
可以使用循环结合指针来打印字符串数组char**,示例代码如下:
#include <iostream>
using namespace std;
int main() {
char* str_arr[] = {"hello", "world", "c++"};
int n = sizeof(str_arr) / sizeof(char*);
for (int i = 0; i < n; i++) {
cout << str_arr[i] << endl;
}
return 0;
}
输出结果:
hello
world
c++
其中,变量n表示字符串数组的长度,使用sizeof运算符获取数组元素的字节数,除以每个元素的字节数(char*的字节数)即可得到数组长度。在循环中,使用指针访问数组元素,并打印出来。注意,每个字符串元素后面需要加上换行符"\n",否则所有字符串会连续输出在一行。
原文地址: https://www.cveoy.top/t/topic/g87u 著作权归作者所有。请勿转载和采集!