为什么 GCC“需要一个表达式”?
#define rows 2 #define cols 2 #define NUM_CORNERS 4 int main(void) { int i; int the_corners[NUM_CORNERS]; int array[rows][cols] = {{1, 2}, {3, 4}}; corners(array, the_corners); for (i = 0; i < 4; i++) printf("%d\n", the_corners[i]); } int corners (int array[rows][cols], int the_corners[]) { the_corners = { array[0][cols-1], array[0][0], array[rows-1][0], array[rows-1][cols-1] }; }
我收到这些奇怪的错误,但我不知道为什么:
prog.c: In function ‘main’:
prog.c:10: warning: implicit declaration of function ‘corners’
prog.c: In function ‘corners’:
prog.c:15: error: expected expression before
#define rows 2 #define cols 2 #define NUM_CORNERS 4 int main(void) { int i; int the_corners[NUM_CORNERS]; int array[rows][cols] = {{1, 2}, {3, 4}}; corners(array, the_corners); for (i = 0; i < 4; i++) printf("%d\n", the_corners[i]); } int corners (int array[rows][cols], int the_corners[]) { the_corners = { array[0][cols-1], array[0][0], array[rows-1][0], array[rows-1][cols-1] }; }
I get these weird errors and i have no idea why:
prog.c: In function ‘main’:
prog.c:10: warning: implicit declaration of function ‘corners’
prog.c: In function ‘corners’:
prog.c:15: error: expected expression before
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
the_corners = { ... }
语法是数组初始化,而不是赋值。我没有方便的标准副本,所以我无法引用章节和诗句,但你想说的是:我还冒昧地将
int Corners
更改为void Corners< /code> 因为你没有返回任何东西。而且您的
main
也需要一个返回值,而您忘记了#include
。The
the_corners = { ... }
syntax is an array initialization, not an assignment. I don't have a copy of the standard handy so I can't quote chapter and verse but you want to say this:I also took the liberty of changing
int corners
tovoid corners
as you weren't returning anything. And yourmain
also needs a return value and you forgot to#include <stdio.h>
.您正在尝试使用初始化表达式作为赋值。即使在 C99 中,这也是无效的,因为 the_corners 的类型是
int*
,而不是int[4]
。在这种情况下,您最好单独分配每个元素。You're trying to use an initialiser expression as an assignment. This isn't valid, even in C99, because the type of the_corners is
int*
, notint[4]
. In this case you would be best off assigning each element individually.main 不知道你的功能。要么将函数 declaration 移至 main 之上,要么将其原型化到 main 之前:
The main doesn' know about your function. Either move the function decleration above the main or prototype it before the main:
试试这个:
您可以在此处阅读有关将 2D 数组传递给函数的信息。
Try this one:
You can read here about passing a 2D array to a function.