c - 在一行上初始化并返回结构
问题描述
是否可以在 C 中的一行上初始化并返回一个结构?
typedef struct {
long tau;
float probability;
} pitch_result;
pitch_result f(){
pitch_result result = {0, 0};
return result;
}
解决方案
好吧,你可以像这样返回一个复合文字:
return (pitch_result) {0, 0};
这也可以与赋值一起使用,不应与初始化混淆。
这不起作用:
pitch_result x;
x = {0, 0};
但这会
pitch_result x;
x = (pitch_result) {0, 0};
复合文字看起来像演员表,但事实并非如此。例如,它是一个左值。这确实编译(但我看不出它有什么用处):
(pitch_result){0, 0} = (pitch_result) {1, 1};
推荐阅读
- mongodb-query - 我们可以在同一个应用程序中同时使用 MongoRepository 和 MongoTemplate
- java - Java - 从 CSV 文件读取错误
- webrtc - 是否可以在不询问用户许可的情况下检测浏览器摄像头?
- android - 当我在我的在线食品订购应用程序中打开食品详细信息时,我的应用程序崩溃了
- javascript - 使用延迟初始化将符号定义为属性
- python - 替换或忽略子导入的文件
- oracle - 使用 SQL 开发人员的 SQL 连接
- docker - 如何使用 nginx 和 terraform 配置两个实例?
- javascript - history.location.replace 不是使用 react-router v4 的函数
- java - getFollowing() 中的错误;应用程序挂起