指向字符混淆的指针

pointer to char confusion

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

我正在复习《c++编程语言》一书中的一些主题。在指针的章节中,Stroustrup有以下例子:

int* pi;             // pointer to int
char* ppc;           // pointer to pointer to char
int* ap[15];         // array of 15 pointers to ints
int (*fp)(char*);    // pointer to function taking a char* argument; returns an int
int* f(char*);       // function taking a char* argument; returns a pointer to int
char* ppc;           // this char has only one *, how can this a pointer to pointer to char?

这是书中的错误还是完全正确?

而不是

char* ppc;           // pointer to pointer to char

应该是

char** ppc;           // pointer to pointer to char

应该是char** ppc;

你在读哪一版?在我的[The.C++.Programing.Language.Special.Edition]中如下:

int* pi; // pointer to int
char** ppc; // pointer to pointer to char
int* ap[15]; // array of 15 pointers to ints
int (*fp) (char *); // pointer to function taking a char* argument; returns an int
int* f(char *); // function taking a char* argument; returns a pointer to int

这一定是书中的打印错误。

您应该有**来获取指向指针的指针