gpt4 book ai didi

没有 va_list 的自定义 printf

转载 作者:太空宇宙 更新时间:2023-11-04 00:51:59 25 4
gpt4 key购买 nike

我正在尝试创建 printf() 的简化版本,但不使用 va_list、va_start、va_arg 和 va_end。

我最初的想法是:

void my_printf (char *format, ...);

然后,检查格式,计算 %(参数)的数量,找出有多少变量传递给了我的函数。从那里开始,我正在考虑根据有多少参数创建一个缓冲区,然后合并并使用 write() 最终输出它们。

有没有更好的方法来解决这个问题?我的计划会遇到问题吗?

如有任何帮助,我们将不胜感激。

我的一些头脑 Storm 代码是:

// Count arguments
int cnt_s, cnt_c, cnt_d, cnt_u, cnt_x;
for (; *format; format++) {
switch(format[0]) {
case '%':
switch(format[1]) {
case 'd':
cnt_d++;
}
}
}

最佳答案

需要va_listva_startva_argva_end 才能访问...

指定的参数

除非你想使用 void* 和 union ----- 呃!

关于没有 va_list 的自定义 printf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15420127/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com