I have functions that are dynamically allocating memories to store strings into char ***
(a 2D array of strings or 3D array of chars).
I later thought that It would be better if I stored them as string literals, so I changed my array to const char ***
. This, however, is giving me errors when I'm using strcpy()
and free()
.
error: passing argument 1 of 'strcpy' discards 'const' qualifier from pointer target type
I understand that the parameters strcpy()
and free()
take are of the type char *
and void *
, but I have also read that it is good practice to store strings into const char *
instead of char *
.
How I am suppose to use string literals if it can't be used for operations like strcpy()
or free()
? Should I have just left it as char ***
instead of const char ***
? There's something obvious here I'm not understanding.
As I have understood your array does not contain pointers to string literals but instead it contains copies of string literals. Otherwise what is strcpy used for? So you may remove the const qualifier from the array (or corresponding pointer) declaration. Take into account that in C string literals have types of non-constant arrays.