1

我在这里有我正在尝试的示例代码。

char test[256], test1[256];

char *combine =("Hello '%s', '%s'",test,test2);

如何将我的测试 test1 的值解析为我的 char *combine?我收到重新声明我的 test 和 test1 没有链接的错误。

4

2 回答 2

2

查看sprintf。它将允许您组合两个字符串。

所以,像:

char combine[LARGE_ENOUGH_NUMBER_HERE]
sprintf(combine, "Hello %s %s", test1, test2);
于 2013-02-03T18:03:49.753 回答
0

该声明:

char *combine = ("Hello '%s', '%s'", test, test2);

一点也不像C。如果要写入格式化字符串,则应使用sprintffamily (来自标准 header <stdio.h>)。您可以在整个 Web 上查看文档。如果您使用 C99,最好使用snprintf,这样更安全。

// C99

#include <stdio.h>

char combine[1024]; /* Should be long enough to hold the string. */
snprintf (combine, sizeof combine, "Hello '%s', '%s'", test, test2);
于 2013-02-03T18:13:12.040 回答