【问题标题】:why does GCC "expect an expression"?为什么 GCC “期待一个表达式”?
【发布时间】:2011-08-16 05:50:26
【问题描述】:
#定义第2行 #define cols 2 #define NUM_CORNERS 4 诠释主要(无效){ 诠释我; int the_corners[NUM_CORNERS]; int 数组[行][列] = {{1, 2}, {3, 4}}; 角落(数组,the_corners); for (i = 0; i

我收到这些奇怪的错误,我不知道为什么:

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 常量使用大写。人们通常不期望小写的东西——特别是如果它们不是函数——是预处理器宏/常量。
  • gotcha - ...这对 C 有什么帮助?

标签: c expression


【解决方案1】:

试试这个:

#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;
}

您可以阅读 here 关于将二维数组传递给函数的信息。

【讨论】:

    【解决方案2】:

    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,因为您没有返回任何内容。而你的main 也需要一个返回值而你忘记了#include &lt;stdio.h&gt;

    【讨论】:

    • 我删除了包含,因为我包含了我自己的包含 stdio.h 的库
    • @tekknolagi:很公平,我只是认为您没有启用所有编译器的警告标志(或者忽略了同样糟糕的警告)并且可能错过了缺少的包含。跨度>
    【解决方案3】:

    您正在尝试使用初始化表达式作为赋值。即使在 C99 中,这也是无效的,因为 the_corners 的类型是 int*,而不是 int[4]。在这种情况下,您最好单独分配每个元素。

    【讨论】:

      【解决方案4】:

      main 不知道你的函数。要么将函数声明移到 main 之上,要么在 main 之前将其原型化:

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

      【讨论】:

      • 好吧,这并没有起到多大作用......它仍然需要在 { 令牌之前有一个表达式
      猜你喜欢
      • 2015-09-20
      • 2010-12-15
      • 2012-07-11
      • 1970-01-01
      • 2014-08-26
      • 1970-01-01
      • 2019-01-18
      • 1970-01-01
      相关资源
      最近更新 更多