Handmade Hero»Forums»Code
max
1 posts
why char * and char** ,char *** are used as string
Edited by max on
how to use char ** ,char*** etc, as string
and how these are stored in computer memory
or we store them in same manner as char array
Mārtiņš Možeiko
2559 posts / 2 projects
why char * and char** ,char *** are used as string
char* in memory is stored exactly as array of char. That's just how any T* type means in C - array of T elements.
Only difference how C commonly treats it as "string" is that it is terminated with char with value 0.
For example, "hello" in memory will be stored as { 'h', 'e', 'l', 'l', 'o', 0 }

There are other ways how to store "string" - for example, by storing pointer to beginning + length in chars. Then there will be no need to terminate it with 0.

char** is very different thing. That is pointer to char*. Which means it could be "array of char*" or "array of strings".
char*** again is different thing. That is pointer to pointer to pointer to char*, which could be "pointer to array of strings".
What exactly it is depends on your code. As any array in C can "pretend" to be any array (properly said - arrays decay to pointers).