0

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.

7
  • The posted code would "work" if str were sufficiently sized for the initialization value you're providing. By "work" I mean compile. Obviously It isn't going to magically plant str into the example3 %s string at the apparent format specifier location. Commented Apr 16, 2022 at 20:28
  • Ah, that magic is what I need. That code compiles anyways, by doesnt work I meant that magic doesnt happen. Also, 6 is enough for handling str as another element. Commented Apr 16, 2022 at 20:31
  • I believe in C++, you can do something like: "bla bla text " + str + "bla bla text". Nothing like that in C? Commented Apr 16, 2022 at 20:32
  • This isn't C++, and no, you don't get that functionality in C. And read what I said again. I didn't say arr wasn't sufficiently sized. I said str is not. The initialization literal "1.0.0.1" you're providing requires 8 chars of storage; you're specifically declaring str as char[6]. 8 doesn't fit into 6, no matter how hard you push. Commented Apr 16, 2022 at 20:50
  • 1
    The undersized array I have pointed out now twice is str, not arr If you removed literally every line from you program except char str[6] = "1.0.0.1"; the problem I'm describing would remain. str is 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 of str is not added to arr[3]. The array arr holds six char pointers, not char arrays. The fifth element in that array, arr[4], is the base address of str. Commented Apr 16, 2022 at 21:10

1 Answer 1

3

... if parsing a variable into one of the strings of the array is possible at declaration.

At compile time, could concatenate as below:

#define STR "1.0.0.1"
char str[] = STR;

char *arr[6] = { 
    "example0",
    "example1",
    "example2",
    "example3" " " STR, // Forms "example3 1.0.0.1"
    "example4"
};

Perhaps OP is interested in something formed during run-time. It uses a variable length array (VLA).

void foobar(const char *str) {
  int n = snprintf(NULL, 0, "example3 %s", str);
  char a[n]; // VLA.
  snprintf(a, sizeof a, "example3 %s", str);

  char *arr[6] = {
      "example0",
      "example1",
      "example2",
      a,
      "example4"
  };

  printf("<%s>\n", arr[3]);
}

int main(void) {
  foobar("1.0.0.1");
}

Output

<example3 1.0.0.>

Alternatively the space for the string could have been done via an allocation.

char *a = malloc(n + 1u);
sprintf(a, "example3 %s", str);
....
free(a);
Sign up to request clarification or add additional context in comments.

11 Comments

I really don't understand why you used char str[] in your example. That is a string constant not a variable.
@ÖmerGök A variable would not work at compile time. You would need something like sprintf("example3 %s", str); which only works at runtime.
^^^ including the target buffer, obviously.
@ÖmerGök char str[] = STR; is illustrative code to show how str and arr can both derive their initializations from STR. str is not needed to initialize arr.
snprintf(arr[3], MAX, "example3 %s", str) wouldn't work anyways. At this point, I think I should just allocate sufficient memory before doing anything.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.