【问题标题】:expected ';' , ' :' or '(' before constant预期的 ';' , ' :' 或 '(' 在常量之前
【发布时间】:2021-12-31 23:29:23
【问题描述】:

#define 中标记了错误,但我不确定问题出在哪里或如何解决。

这可能充满了错误,所以欢迎任何反馈。

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define N 6

void fil_array(int* A,int const N);
void print_array(int* A,int const N);
    
int main()
{
    int i,A[N];
    srand(time(NULL));
    fil_array(A, N);
    print_array(A, N);
    return 0;
}

void fil_array(int* A, int const N)
{
    int i;

    for (i=0; i<N; i++)
        A[i]=rand%21;
}

void print_array(int* A, int const N)
{
    int i;

    for (i=0; i<N; i++)
        printf("A[%d]=%d\n", i,A[i]);
}

【问题讨论】:

标签: c function


【解决方案1】:

您的代码中存在命名冲突。宏名称#define N 6int const N 这2 个名称有冲突。使用不同的名称来解决这个问题。还有一件事我想分享。在这一行中,A[i]=rand%21; rand 应该是一个函数名。正确的方式是A[i]=rand()%21;

【讨论】:

    【解决方案2】:

    您应该将此参数设为int n,而不是int const N - 选择与宏N 不同的名称。

    【讨论】:

    • 我试过了,但没用,还是老是给我同样的问题,是我笨
    • Nvm 我知道了,谢谢
    【解决方案3】:

    我猜问题是您将函数的参数命名为前处理器定义的常量。我说的是N。 当编译链启动时,预处理器会替换代码中已定义常量的值,这意味着您的函数原型如下所示:

    void fil_array(int* A,int const 6);
    

    因为你告诉编译器 N 表示 6

    #define N 6
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-03
      • 1970-01-01
      • 1970-01-01
      • 2013-02-10
      • 1970-01-01
      相关资源
      最近更新 更多