【发布时间】:2021-03-08 16:59:53
【问题描述】:
这是一个 C 函数,它返回一个在结构中定义的类型的数组。它成功返回数组 p。我的问题:我可以消除 p 并按照注释掉的语句中所示的方式返回吗?
#include<stdio.h>
typedef struct {
int dy, dx, iter;
} M;
M *boom (int x) {
// I'm happy with this approach
static M p[] = { {+5, +6, +7}, {+1, +1, +0}, {+3, +3, +3} };
return p;
// but I'm keen to know how this can be done.
// return { {+5, +6, +7}, {+1, +1, +0}, {+3, +3, +3} }; // causes error
}
int main() {
M *result = boom(1);
printf("First element %d\n", result[2].dy);
}
对于我已注释掉的行,我收到“错误:'{' 标记之前的预期表达式”。
【问题讨论】: