简体   繁体   English

char ***在C中是什么意思?

[英]What does char*** mean in C?

I need help understanding what char*** means and how do I initialize a variable that is of type char***. 我需要帮助来了解char ***的含义以及如何初始化char ***类型的变量。

For example, if there is a function that reads the lines of a file, while keeping track of the number of lines and printing out each line with its corresponding number: 例如,如果有一个函数可以读取文件的行,同时跟踪行数并打印出每行及其对应的数字:

void read_lines(FILE* fp, char*** lines, int* num_lines){}

What would char*** represent in this case and how would I initialize the variable lines? 在这种情况下char ***代表什么,我将如何初始化变量行?

It's a pointer-to-pointer-to-pointer-to- char . 这是一个指向char的指针。 In this case, it's very likely to be an output parameter. 在这种情况下,很有可能是输出参数。 Since C passes arguments by value, output parameters require an extra level of indirection . 由于C按值传递参数,因此输出参数需要额外的indirect级别。 That is, the read_lines function wants to give the caller a char** , and to accomplish that via an output parameter, it needs to take a pointer to a char** . 也就是说, read_lines函数要给调用方一个char** ,并且要通过一个输出参数来实现这一点,它需要使用一个指向 char**指针 Likely all you'd need to do to invoke it is: 调用它可能需要做的只是:

char** lines = null;
int num_lines;
read_lines(fp, &lines, &num_lines);

Also see C Programming: malloc() inside another function . 另请参见C编程:另一个函数中的malloc()

I need help understanding what char*** means ... 我需要帮助来了解char ***的含义...

The char*** type is a pointer. char***类型是一个指针。 A pointer to a char ** . 指向char **指针。 p as pointer to pointer to pointer to char p作为指向char的指针的指针

char*** p;

... and how do I initialize a variable that is of type char***. ...以及如何初始化char ***类型的变量。

char*** p1 = NULL;  // Initialize p with the null pointer constant.

char *q[] = { "one", "two", "three" };
char*** p2 = &q;  // Initialize p2 with the address of q

char ***p3 = malloc(sizeof *p3);  // Allocate memory to p3.  Enough for 1 `char **`.
....
free(p3); // free memory when done.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM