Thursday, February 23, 2023
A breakdown of the triple-star pointer
I few days ago I read “Lessons learnt while trying to modernize some C code”
and one of the problems of C stated stood out to me: “Avoid constructs like
char ***. I thought it was a joke, but people do
char *** and it’s insane—almost impossible to comprehend why
do you need a pointer to a pointer to a pointer.”
but come on!
That doesn't happen often enough to complain about!
And then I found one in my own code!
at least I can explain why I needed a
It's not insane,
and it's not impossible to comprehend why.
I'll start with
that means “string”
(the exceptions are just that—exceptions).
We can replace
char * with
typedef char *cstring which gets rid of one “*”,
when you see
int main(int argc,char **argv),
it generally has the meaning of an array of strings:
int main(int argc,char *argv).
Sometimes it could mean just a pointer to a pointer,
but I'm using the “array of strings” meaning in my code.
Translated using the custom type I defined above,
char ** becomes becomes
cstring  and
char *** becomes
cstring *—a pointer to an array of strings.
And this idiom,
when it happens,
usually means the callee is going to allocate the memory for the array of strings and return it via the pointer.
Which is exactly what the function I wrote does.
So when I expect a
char *** here,
what I'm asking for is a pointer to an array of strings
(aka character pointers or character arrays).
The only thing insane about this is the syntax,
and maybe the semantics
(pointers and arrays are near enough the same that it's dangerous)
but I've been working with C long enough that I just kind of accept it.
just don't ask about
char ****—that's just silly talk!