-2

So , i'm trying to make a simple program in C using arrays.

int odd(int v1[],int n) {
    int v2[n];
    int i;
    for (i=0;i<n;i++) {
        if (v1[i]%2==0) {
            v2[i]=v1[i];
        }
        else {
            v2[i]=v1[i]*2;
        }
    }
    for (i=0;i<n;i++) {
        printf("Array %d",v2[i]);
    }
    return 0;
}

int main() {
    odd({1,2,3,4,5},5);
    return 0;
}

I get an error in the main function ('Expected expression') and i don't know how to correct.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
k3nz0
  • 7
  • 6
  • 2
    You need to call `odd` with an array. `{1,2,3,4,5}` is not an array. It is merely a *list* that is not an expression, and it can only be used for initialization. – Kerrek SB Apr 22 '17 at 17:29

1 Answers1

3

An expression like

 {1,2,3,4,5}

is not an array, on it's own. It's a brace-enclosed list, which is primarily used for initialization purpose.

If you don't want to define a separate array variable, you need to make use of compound literal. Something like

 odd((int[]){1,2,3,4,5},5);

will do the job.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261