为什么 GCC“需要一个表达式”?

发布于 2024-11-04 04:17:18 字数 683 浏览 0 评论 0原文

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

星星的轨迹 2024-11-11 04:17:18

the_corners = { ... } 语法是数组初始化,而不是赋值。我没有方便的标准副本,所以我无法引用章节和诗句,但你想说的是:

void corners (int array[rows][cols], int the_corners[]) {
    the_corners[0] = array[0][cols-1];
    the_corners[1] = array[0][0];
    the_corners[2] = array[rows-1][0];
    the_corners[3] = array[rows-1][cols-1];
}

我还冒昧地将 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:

void corners (int array[rows][cols], int the_corners[]) {
    the_corners[0] = array[0][cols-1];
    the_corners[1] = array[0][0];
    the_corners[2] = array[rows-1][0];
    the_corners[3] = array[rows-1][cols-1];
}

I also took the liberty of changing int corners to void corners as you weren't returning anything. And your main also needs a return value and you forgot to #include <stdio.h>.

绿光 2024-11-11 04:17:18

您正在尝试使用初始化表达式作为赋值。即使在 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*, not int[4]. In this case you would be best off assigning each element individually.

若相惜即相离 2024-11-11 04:17:18

main 不知道你的功能。要么将函数 declaration 移至 main 之上,要么将其原型化到 main 之前:

int corners (int array[rows][cols], int the_corners[NUM_CORNERS]);

The main doesn' know about your function. Either move the function decleration above the main or prototype it before the main:

int corners (int array[rows][cols], int the_corners[NUM_CORNERS]);
好菇凉咱不稀罕他 2024-11-11 04:17:18

试试这个:

#include <stdio.h>
#define NROWS 2
#define NCOLUMNS 2
#define NCORNERS 4

int corners(int (*arr)[NCOLUMNS], int* the_corners);

int main() {
    int i;
    int the_corners[NCORNERS];
    int arr[NCOLUMNS][NROWS] = {{1, 2}, {3, 4}};

    corners(arr, the_corners);

    for (i = 0; i < NCORNERS; i++)
        printf("%d\n", the_corners[i]);

    return 0;
}

int corners(int (*arr)[NCOLUMNS], int* the_corners) {

        the_corners[0] = arr[0][NCOLUMNS-1];
        the_corners[1] = arr[0][0];
        the_corners[2] = arr[NROWS-1][0];
        the_corners[3] = arr[NROWS-1][NCOLUMNS-1];

        return 0;
}

您可以在此处阅读有关将 2D 数组传递给函数的信息。

Try this one:

#include <stdio.h>
#define NROWS 2
#define NCOLUMNS 2
#define NCORNERS 4

int corners(int (*arr)[NCOLUMNS], int* the_corners);

int main() {
    int i;
    int the_corners[NCORNERS];
    int arr[NCOLUMNS][NROWS] = {{1, 2}, {3, 4}};

    corners(arr, the_corners);

    for (i = 0; i < NCORNERS; i++)
        printf("%d\n", the_corners[i]);

    return 0;
}

int corners(int (*arr)[NCOLUMNS], int* the_corners) {

        the_corners[0] = arr[0][NCOLUMNS-1];
        the_corners[1] = arr[0][0];
        the_corners[2] = arr[NROWS-1][0];
        the_corners[3] = arr[NROWS-1][NCOLUMNS-1];

        return 0;
}

You can read here about passing a 2D array to a function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文