Home > Back-end >  expected ';' , ' :' or '(' before constant
expected ';' , ' :' or '(' before constant

Time:11-22

The error is marked in the #define, but I'm unsure where is the problem or how to fix it.

This is probably filled with mistakes, so any feedback is welcome.

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

CodePudding user response:

There is naming conflict in your code. Macro name #define N 6 and int const N this 2 name are conflicted. Use different name to solve this. There is one more things I want to share. In this line A[i]=rand!; rand should be a function name. The correct way is A[i]=rand()!;

CodePudding user response:

Instead of int const N you should make this parameter int n - pick a different name than the macro N.

CodePudding user response:

I guess the problem is you are naming parameters of your function as a constant defined by prepocessor. I am talking about N. When compilation chain starts, the preprocessor substitute the value of defined constant in your code, this means that your function prototypes appear like:

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

Because you tell the compiler that N means 6

#define N 6
  • Related