Array Element is a Variable in Array of Strings in C -
i'm trying initialize array of strings in c. want set 1 of elements of array variable, i'm getting compiler error. what's wrong this?
char * const app_name = "test_app"; char * const array_of_strings[4] = { app_name, "-f", "/path/to/file.txt", null };
the error error: initializer element not constant
.
the standard distinguishes const
-qualified variables , compile time constants.
evaluating variable (app_name
) not considered compile time constant in sense of c standard. this
char const app_name[] = "test_app"; char const*const array_of_strings[4] = { &app_name[0], "-f", "/path/to/file.txt", 0, };
would allowed, since not evaluating app_name
taking address.
also, should treat string literals if had type char const[]
. modifying them has undefined behavior, should protect doing so.
Comments
Post a Comment