char *strArray[3] = {"hello", "world", "!"};
时间: 2023-09-20 13:08:59 浏览: 28
This is an array of 3 strings (char arrays). The first element is "hello", the second is "world", and the third is "!". Each string is terminated with a null character ('\0') to indicate the end of the string.
Note that since the array is of type "char *", each element is actually a pointer to a char array. This means that the strings are stored as separate arrays in memory, and the array "strArray" simply holds pointers to the start of each string.
相关问题
c++char **
C++中的char **是一个指向字符指针的指针,也可以看作是一个二维字符数组。它通常用于表示字符串数组或者字符指针数组。
具体来说,char **可以用来存储多个字符串,每个字符串都是一个字符指针。这样的数据结构可以用于处理多个字符串的情况,比如读取文件中的多行文本,或者存储命令行参数等。
下面是一个简单的示例代码,展示了如何使用char **来存储和访问字符串数组:
```cpp
#include <iostream>
int main() {
const char* strings[] = {"Hello", "World", "C++"};
int size = sizeof(strings) / sizeof(strings[0]);
char** strArray = new char*[size];
for (int i = 0; i < size; ++i) {
strArray[i] = new char[strlen(strings[i]) + 1];
strcpy(strArray[i], strings[i]);
}
for (int i = 0; i < size; ++i) {
std::cout << strArray[i] << std::endl;
}
for (int i = 0; i < size; ++i) {
delete[] strArray[i];
}
delete[] strArray;
return 0;
}
```
在上面的代码中,我们首先定义了一个字符串数组`strings`,然后通过动态内存分配创建了一个char **类型的数组`strArray`,并将每个字符串拷贝到`strArray`中。最后,我们遍历`strArray`并输出每个字符串,然后释放内存。
C++ const char*的数组表达式
C++中的const char*数组表达式是指一个指向字符常量的指针数组。它可以用来表示多个以null结尾的字符串。
例如,以下是一个const char*数组表达式的示例:
```cpp
const char* strArray[] = { "Hello", "World", "ChitGPT" };
```
在上面的示例中,strArray是一个包含3个指向字符常量的指针的数组。每个指针指向一个以null结尾的字符串。
你可以通过使用索引来访问数组中的特定字符串,例如:
```cpp
cout << strArray[0] << endl; // 输出 "Hello"
cout << strArray[1] << endl; // 输出 "World"
cout << strArray[2] << endl; // 输出 "ChitGPT"
```
请注意,const char*数组表达式中的指针指向的字符常量是不可修改的。如果你尝试修改它们,可能会导致未定义的行为。