指向指向字符数组的指针数组的指针

Pointer to array of pointers to array of char

本文关键字:数组 指针 字符      更新时间:2023-10-16

我目前正在研究使用 Koenig 的加速C++C++,我在指向初始指针数组的指针方面遇到了一些麻烦。书中说,以下代码


int main(int argc, char** argv)
{
// if there are arguments, write them
if (argc > 1) {
int i; // declare i outside the for because we need it after the loop finishes
for (i = 1; i < argc-1; ++i)
// write all but the last entry and a space
cout << argv[i] << " ";
// argv[i] is a char*
cout << argv[i] << endl;
}
return 0;
}

当使用"Hello, world"参数运行时会产生 Hello World,但如果argv是指向指针数组的指针,那么argv[i]不应该是指向char数组的指针,该数组的输出是内存地址而不是数组本身?

char*是"C-string"——它是字符串在C语言中的表示方式。std::cout知道如何打印char*,因为<<运算符的实现过载。

C++还具有std::string类型,这是表示字符串的另一种方式。

char** argv

char* argv[]相同

在 C++ 中引用数组的变量只是指向它的第一个元素的指针。

Argv 基本上是一个 C 字符串数组,它们是字符数组。

Argc 告诉你 C 字符串 argv 指向的数量。

您不需要知道 C 字符串 (char[]) 有多长,因为它们以 null 结尾。它只是从初始内存地址开始逐个字符读取字符串字符,直到它到达空终止符。