asprintf
来源:互联网 发布:小小食杂铺 知乎 编辑:程序博客网 时间:2024/06/05 10:56
函数原型:
int asprintf(char **strp, const char *fmt, ...);
功能:
asprintf()可以说是一个增强版的sprintf(),在不确定字符串的长度时,非常灵活方便,能够根据格式化的字符串长度,申请足够的内存空间。此外,使用完后,必须通过free()释放空间。不过,这是GNU扩展的C函数库,不是标准C函数库或者POSIX。
头文件:
asprintf.h
char *cli_command_name(struct cli_def *cli, struct cli_command *command)
{
char *name = cli->commandname;
char *o;
if (name) free(name);
if (!(name = calloc(1, 1)))
return NULL;
while (command)
{
o = name;
if (asprintf(&name, "%s%s%s", command->command, *o ? " " : "", o) == -1)
{
fprintf(stderr, "Couldn't allocate memory for command_name: %s", strerror(errno));
free(o);
return NULL;
}
command = command->parent;
free(o);
}
cli->commandname = name;
return name;
}
{
char *name = cli->commandname;
char *o;
if (name) free(name);
if (!(name = calloc(1, 1)))
return NULL;
while (command)
{
o = name;
if (asprintf(&name, "%s%s%s", command->command, *o ? " " : "", o) == -1)
{
fprintf(stderr, "Couldn't allocate memory for command_name: %s", strerror(errno));
free(o);
return NULL;
}
command = command->parent;
free(o);
}
cli->commandname = name;
return name;
}
0 0
- asprintf
- 用 snprintf / asprintf 取代不安全的 sprintf
- 用 snprintf / asprintf 取代不安全的 sprintf
- 用 snprintf / asprintf 取代不安全的 sprintf
- 用 snprintf / asprintf 取代不安全的 sprintf
- asprintf的头文件没定义
- sprintf函数、snprintf函数、asprintf函数、vsprintf
- 用 snprintf / asprintf 取代不安全的 sprintf
- newlib 中的打印函数printf,sprintf及asprintf
- implicit declaration of function 'asprintf' 警告的解决
- SELinux
- 事务的传播特性
- CCD工作原理
- hibernate的NativeSql查询
- 一对多关联关系(外键)
- asprintf
- 2016.01.22-2016.01.24回顾
- Android开发实现异步加载图片
- goldengate ERROR OGG-01172
- velocity语法大全
- 深度学习(四)卷积神经网络Lenet-5实现
- bzoj2012[hnoi2005]狡猾的商人
- mingw(gcc)编译openjpeg
- ViewPager的使用2