Christina
Christina

Reputation: 61

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***.

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?

Upvotes: 0

Views: 2472

Answers (2)

chux
chux

Reputation: 154602

I need help understanding what char*** means ...

The char*** type is a pointer. A pointer to a char **. p as pointer to pointer to pointer to char

char*** p;

... and how do I initialize a variable that is of type 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.

Upvotes: 2

jamesdlin
jamesdlin

Reputation: 90184

It's a pointer-to-pointer-to-pointer-to-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. 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**. 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.

Upvotes: 2

Related Questions