将 struct 字段转换为 unsigned char 字符串

标签 c struct

我在嵌入式系统上,所以无法访问大多数标准库。我有一个包含 char 值的 struct

我有一个简单的 print 函数,它只是将一个 unsigned char 字符串输出到附加的屏幕。它不像 printf 那样支持格式说明符。

这是结构:

typedef struct my_options{
    char test;
} my_options;

这是我尝试输出值的地方:

 struct my_options options;
 print(options.test);  //Here I get "implicit conversion of int to ptr"

我如何实现这一目标?

最佳答案

创建一个 char 数组来保存你的 char 然后打印它:

char wrapper[2];
wrapper[0] = options.test;
wrapper[1] = '\0';
print(wrapper);

关于将 struct 字段转换为 unsigned char 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22453763/

相关文章:

haskell - 如何在haskell中为 "unpack"结构

c++ - C++中的结构填充

c# - 我应该为我的 Linq2Sql 数据使用 Struct 而不是轻量级数据类吗?

c++ - Linux 上的 AIO 支持

c - C之前用于操作系统开发的语言

JSON 嵌套动态结构 Go 解码

将值分配给结构中的地址时出现 C 段错误

c - 如何在 C 中从 char * 转换为 int 到十六进制十进制

c - BMP 颜色替换器 : Algorithm for padding doesn't work

c - 读取保存文件时,链接类型 1 不支持入站/出站