Your other option, aside from the standard declaration of a as any array of chars is to use a compound literal to initialize your pointer as a pointer to an array as opposed to a pointer to string-literal. C99 introduced the compound literal that allows you to initialize a pointer to a specified type. It has the general form of a cast followed by an initializer. In your case, you would initialize your single pointer within your array of pointers as,
char *a[1] = { (char[]){"Test"} };
The compound-literal being:
(char[]){"Test"} /* which is the literal "Test" initialized as an array */
This initializes the pointer element a[0] as a pointer to array instead of pointer to string-literal. Your code would then look like:
#include <stdio.h>
int main (void) {
char *a[1] = { (char[]){"Test"} };
printf("%c\n", a[0][2]); /* output 's' */
a[0][2] = 'e'; /* no Segfault here */
printf("%c\n", a[0][2]); /* output 'e' */
return 0;
}
Example Use/Output
$ ./bin/cmplitptp
s
e
As noted in several cases, your choice of declaring a as an array of pointers to char with one-element, is a bit out of the ordinary. Generally speaking if you wanted to initialize an array of characters, you would simply declare char a[] = "Test";. However, you can also declare a as a simple pointer to char and use a compound literal to initialize that pointer as a pointer to array, e.g. char *a = (char[]){ "Test" };, which would eliminate one-level of indirection on a simplifying your code to:
#include <stdio.h>
int main (void) {
char *a = (char[]){ "Test" };
printf("%c\n", a[2]); /* output 's' */
a[2] = 'e'; /* no Segfault here */
printf("%c\n", a[2]); /* output 'e' */
return 0;
}
(same output)
While it practice you will generally see a compound literal used to initialize a struct, there is no reason they cannot be used to initialize an array. Add it to your C-toolbox, and, in practice, if you want a as an array initialized to a specific string, just use char a[] = "Some String";.
char *a[1] = { (char[]){"Test"} };char c[] = "Str"; char* a[1] = {c};.