Sounds like what you are fishing for is so-called "X macros". Please note that these should be avoided as far as possible, since they make the code very hard to read.
The most correct solution here is to simply use an array. If you find yourself needing something else, the root cause is likely bad program design. Other posted answers show proper solutions with plain arrays. X macros should be the last resort for very special cases.
That being said, this is how you achieve this with X macros:
#include<stdio.h>
#define STRING_LIST \
X(0, "aaasa") \
X(1, "bssbaabb") \
X(2, "bbbssbaaaa") \
X(3, "bcccbaabaab")
int main(void)
{
// declare pointers s0 to s3:
#define X(i, str) const char* s##i = str;
STRING_LIST
#undef X
// print data by using pointers s0 to s3:
#define X(i, str) printf("s%d: %s \n", i, s##i);
STRING_LIST;
#undef X
}
If you want to combine this with a loop/array, it is also possible, if we go "full C retard" and do something like this...
// Definitely NOT recommended practice but can be studied for learning purposes
#include<stdio.h>
#define STRING_LIST \
X(0, "aaasa") \
X(1, "bssbaabb") \
X(2, "bbbssbaaaa") \
X(3, "bcccbaabaab")
// determine the size of the X macro list by using an enum:
typedef enum
{
#define X(i, str) DUMMY_##i,
STRING_LIST
#undef X
STRINGS_N
} strlist_size_t;
// declare union with both pointers s0 to s3 and an array:
typedef union
{
struct // C11 anonymous struct
{
#define X(i, str) const char* s##i;
STRING_LIST
#undef X
};
const char* array [STRINGS_N];
} strlist_t;
int main(void)
{
// ensure that the type punning is safe on the given system:
_Static_assert(sizeof(strlist_t) == sizeof(const char* [STRINGS_N]),
"Struct padding detected! Type punning failed.");
// initialize list:
strlist_t strlist =
{
#define X(i, str) .s##i = (str),
STRING_LIST
#undef X
};
// print data by using pointers s0 to s3:
#define X(i, str) printf("s%d: %s \n", i, strlist.s##i);
STRING_LIST;
#undef X
printf("\n");
// print data using a loop:
for(int i=0; i<STRINGS_N; i++)
{
printf("s%d: %s \n", i, strlist.array[i]);
}
}