c - 在 C 中打印自定义对象
问题描述
struct
在 C中打印 a 的最常见方法是什么?例如,这是我目前正在做的打印Book
:
typedef struct {
char* title;
unsigned int year;
} Book;
void print_book(Book book)
{
printf("{\n\ttitle: \"%s\",\n\tyear: %d\n}\n", book.title, book.year);
}
int main(int argc, char * argv[])
{
Book hamlet;
hamlet.title = "The Tragedy of Hamlet, Prince of Denmark";
hamlet.year = 1603;
print_book(hamlet);
return 0;
}
{
title: "The Tragedy of Hamlet, Prince of Denmark",
year: 1603
}
编写自定义独立函数(例如print_book
)是最常见的方法吗?或者有没有办法将“打印”方法更紧密地绑定到结构/类型定义?
解决方案
推荐阅读
- node.js - 如何使用 ts-sinon 存根嵌套依赖项
- flutter - 颤振未处理的异常:类型“(动态)=>动态”不是“测试”类型“(动态)=>布尔”的子类型
- sql - SOQL/SQL:左外连接查询的结果不正确
- c# - 通过DataSet.Clear(),DataTable 和DataColumn 和DataRow 也被删除了?
- go - 具有工作池的基准功能,没有工作人员但速度更快,用于测试结果
- visual-studio - 初始化模板 Visual Studio 2019 时遇到错误
- python - 如何结束周长计算循环
- android - 如何在 Flutter 的 WebView 中使用 post 方法
- android - 通过我的 Flutter 应用与第三方应用交互
- blazor - 托管的 Blazor wasm 会在它们是处理服务器端页面的控制器操作时不断加载 Blazor 应用程序