gpt4 book ai didi

c - 使用 void* 指针将未知/混合类型传递给 printf() (一种在运行时推断类型)

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

我正在尝试将混合类型传递给 printf()。在实际代码中(底部),我还传递了格式占位符,以便 printf() 知道如何格式化。

当我不小心直接使用这些 void* 指针作为值调用时,我发现我的代码没有按预期工作。

所以我创建了一个测试程序,进行了搜索和探索,直到我想出了这个解决方案。

#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

void test1(void* a) {
printf("value: %d\n", *(int*)a);
}

void test2(void* a) {
printf("value: %f\n", *(float*)a);
}

int main(int argc, char** argv) {
int* aip = &(int){1};
test1(aip);

int ai = 1;
test1(&ai);

float af = 1.75;
test2(&af);

return 0;
}

但我不想硬编码 *(int*)*(float*) 取消引用和强制转换,但实际上是这样写的:

void test(void* a) {
printf("value: %VARIES\n", a);
}

VARIES 被传递到 test() 并连接到最终的格式化程序字符串中。

实际的功能是这样的:

void fn(void* value, char* fmtChar) {
char fmtHelper[50];
// fill fmtChar in formatter string's %s placeholder
snprintf(fmtHelper, sizeof(fmtHelper), "value : %s\n", fmtChar);
// actually print value varying argument
printf(fmtHelper, value);
}

我当前的解决方法是使用 switch/case 语句并以编程方式确定转换和格式化程序占位符。

最佳答案

如果你的目标是当前的 C (C11),你可以使用带有宏的泛型表达式来创建泛型函数:

#include <stdio.h>

void print_int(int x) {
printf("an int: %d\n", x);
}

void print_double(double x) {
printf("a double: %f\n", x);
}

#define print(X) _Generic((X), \
float: print_double, \
double: print_double, \
default: print_int \
)(X)

int main(void) {
print(1); // an int: 1
print(1.0); // a double: 1.00000
}

关于c - 使用 void* 指针将未知/混合类型传递给 printf() (一种在运行时推断类型),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37260030/

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