In C we cannot assign a char**
to const char *const *
because of this problem. So, what's the base way to declare a function accepting an array of strings?
My input parameters may be of any types, including:
char **
const char **
- ...
In C we cannot assign a char**
to const char *const *
because of this problem. So, what's the base way to declare a function accepting an array of strings?
My input parameters may be of any types, including:
char **
const char **
I end up using the Antti Haapala's suggestion (thank you Antti Haapala!). Using const char * const *
as the argument type and explicitly casting incompatible types.
#define STR_ARR(v) _Generic((v), \
char**: (const char *const *)(v), \
char *const *: (const char *const *)(v), \
default: (v))
void foo(const char * const * strings);
Usage:
char **strs1;
foo(STR_ARR(strs1));
const char **strs2;
foo(STR_ARR(strs2));
The best way is to have the caller sort their code out. Otherwise you can do a type-safe wrapper macro along the lines of this:
#include <stdio.h>
void str_print_ro (const char* str_arr[], size_t n)
{
for(size_t i=0; i<n; i++)
{
puts(str_arr[i]);
}
}
#define str_print(arr,n) _Generic((arr), \
const char** : str_print_ro, \
char** : str_print_ro) ((const char**)(arr), n)
int main (void)
{
char* arr[] = {"hello", "world"};
str_print(arr, sizeof arr/sizeof *arr);
}
It probably doesn't make much sense to const-qualify the pointers themselves. If you for some reason need that too, then const char* const str_arr[]
should sort it, and that one can be converted to from char**
and const char**
both.