C将字符串解析成一系列字符串

发布于 2025-01-22 07:23:01 字数 531 浏览 0 评论 0原文

在定义一系列字符串的同时,我通常会声明它类似于以下内容:

    char *arr[5] =
{
    "example0",
    "example1",
    "example2",
    "example3",
    "example4"
};

我遇到问题的地方是我不知道如何将变量传递到arr的元素之一中。

例如,

char str[6] = "1.0.0.1";

char *arr[6] =
{
    "example0",
    "example1",
    "example2",
    "example3 %s", str,
    "example4"
};

当然,这无效,这只是我遇到的问题的基本说明。

我也知道我以后可以使用strncat()或什至snprintf(),但是,为避免使用这些记忆的痛苦,我只想知道是否解析变量在声明时,可以进入阵列的一个字符串。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拥抱没勇气 2025-01-29 07:23:02

...如果在声明中可以将变量解析到数组的一个字符串中。

在编译时,可以如下:

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

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

OP对运行时形成的东西感兴趣。它使用变量长度阵列(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");
}

或者

<example3 1.0.0.>

,可以通过分配来完成 string 的空间。

char *a = malloc(n + 1u);
sprintf(a, "example3 %s", str);
....
free(a);

... 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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文