这是用C我的代码片段:]为什么不在C中显示?
char *str = NULL;
int len = -1;
// Get length
len = snprintf(NULL, 0, "[%d]", 2);
// Allocate str
str = (char *)malloc(len + 1);
// Assign str
snprintf(str, len, "[%d]", 2);
assert(len == 3);
// Display str
puts(str);
我希望这应该显示[2]
。而len
这里是3
但运行此代码只显示[2
为什么会这样呢?
请[不要在C](http://stackoverflow.com/a/605858/28169)中投射'malloc()'的返回值。 – unwind