While defining an array of strings, I usually declare it similar to the following:
char *arr[5] =
{
"example0",
"example1",
"example2",
"example3",
"example4"
};
Where I'm having a problem is I don't know how to pass a variable into one of the elements of arr.
For instance,
char str[6] = "1.0.0.1";
char *arr[6] =
{
"example0",
"example1",
"example2",
"example3 %s", str,
"example4"
};
Of course, this doesn't work, it's just a basic illustration of what I'm having trouble with.
I also know I can later use strncat() or even snprintf() but, to avoid the pain of handling memory with those, I just want to know if parsing a variable into one of the strings of the array is possible at declaration.
strwere sufficiently sized for the initialization value you're providing. By "work" I mean compile. Obviously It isn't going to magically plantstrinto theexample3 %sstring at the apparent format specifier location."bla bla text " + str + "bla bla text". Nothing like that in C?arrwasn't sufficiently sized. I saidstris not. The initialization literal"1.0.0.1"you're providing requires 8 chars of storage; you're specifically declaringstraschar[6]. 8 doesn't fit into 6, no matter how hard you push.str, notarrIf you removed literally every line from you program exceptchar str[6] = "1.0.0.1";the problem I'm describing would remain.stris undersized. You cannot shove 8 chars into a char buffer declared to be 6. If there is a way I can say that differently than I already have, it eludes me. And the sizeof ofstris not added toarr[3]. The arrayarrholds six char pointers, not char arrays. The fifth element in that array,arr[4], is the base address ofstr.