C语言 从 C 中的变量构建字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4881937/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Building Strings from variables in C
提问by Raven Dreamer
I'm working on a bare-bones BlackHyman game that uses sockets, courtesy of my Operating Systems class. We were given a socket interface already which passes an array of characters back and forth.
我正在开发一个使用套接字的基本二十一点游戏,由我的操作系统类提供。我们已经获得了一个套接字接口,它来回传递字符数组。
I had hopedI could do something like this:
我曾希望我能做这样的事情:
char[] msgOut = printf("Dealer's Card is %C %C", char1, char2);
sendMsg(msgOut);
HOWEVER, googling lead me to determine that the return value of printf is an int of the number of Char's output, nota char[] of the chars themselves (as I had hoped).
然而,谷歌搜索使我确定 printf 的返回值是 Char 输出数量的 int,而不是字符本身的 char[] (正如我所希望的)。
Is there another C function that lets me build strings from my variables?
是否有另一个 C 函数可以让我从我的变量构建字符串?
回答by Ed S.
printfwrites to standard output. snprintfaccomplishes what you are going for here. The interpolated string is stored in 'buffer' after the call to snprintf. You may want define your buffer size a little more intelligently, but this is just an example.
printf写入标准输出。snprintf完成你在这里要做的事情。调用 snprintf 后,插入的字符串存储在“缓冲区”中。您可能希望更智能地定义缓冲区大小,但这只是一个示例。
char buffer[1024];
snprintf(buffer, sizeof(buffer), "Dealer's Card is %C %C", char1, char2);
回答by ephemient
Glibc (and several other C libraries) have a convenience function asprintf.
Glibc(和其他几个 C 库)有一个方便的函数asprintf。
char *msgOut;
asprintf(&msgOut, "Dealer's Card is %C %C", char1, char2);
sendMsg(msgOut);
free(msgOut);
This is most useful when you do not have a good advance prediction for the amount of memory that is required to hold the string. (If you do, snprintfhas less overhead, as it does not dynamically allocate memory.)
当您对保存字符串所需的内存量没有很好的预先预测时,这最有用。(如果这样做,snprintf开销会更少,因为它不会动态分配内存。)
On systems without asprintf, but with a standards-compliant snprintf, it can be implemented by two calls to snprintf— first with no buffer and zero size to determine the string length, an intervening malloc, and then a second time using that newly allocated space.
在没有asprintf,但符合标准的系统上snprintf,它可以通过两次调用来实现snprintf- 首先没有缓冲区和零大小来确定字符串长度,中间的malloc,然后第二次使用新分配的空间。
回答by frmdstryr
If you want a string builder in c that dynamically allocates memory I found http://linux.die.net/man/3/vasprintfto be useful.
如果你想要一个动态分配内存的 c 中的字符串生成器,我发现http://linux.die.net/man/3/vasprintf很有用。
Example:
例子:
#include <stdio.h>
int i;
printf("//Simple dynamic string builder:\n");
char *strs[6] = {"I","am","an","array","of","strings"};
char *buf = "["; // open bracket
for (i=0;i<6;i++) {
// Dynamically build and allocate memory
asprintf(&buf,"%s%s",buf,strs[i]);
if(i!=5) { // Add a comma , after each but skip the last
asprintf(&buf,"%s,",buf);
}
}
asprintf(&buf,"%s]",buf); // closing backet
printf("\"%s\"\n",buf);
The output is
输出是
//Simple string builder:
"[I,am,an,array,of,strings]"
so char *bufis dynamically being expanded by asprintfand is building by formatting itself into the asprintfstatement.
sochar *buf被动态扩展asprintf并通过将自身格式化到asprintf语句中来构建。

