Alexxx
Alexxx

Reputation: 299

syntax if else inside function

I have simple else if and have errors on the word syntax - for. Please help me to fix this

Error   4   error C2143: syntax error : missing ';' before 'type'   
Error   7   error C2143: syntax error : missing ';' before '{'
Error   3   error C2143: syntax error : missing ')' before 'type'   
Error   6   error C2059: syntax error : ')' 

My code is checking which array is bigger and puts the bigger. Thats my all fnction:

void PrintIdentical(...)
{
    int i;
    int smaller;


     ...

    for (i = 0; i < smaller; i++)
    {
        printf ("%d", arrA[i]);
       printf ("%d", arrB[i]);
    }
}

Upvotes: 0

Views: 171

Answers (4)

ob_dev
ob_dev

Reputation: 2838

void enticl(int arrA[], int arrA_size, int arrB[], int arrB_size)
{
    int i;
    int smaller;
    int *arr;

    if(arrA_size>arrB_size)
    {
        smaller=arrB_size;
        arr = arrB;
    }
    else
    {
        smaller=arrA_size;
        arr = arrB;

    }

    for(i = 0; i < smaller; i++) 
    {
        printf("%d\n", arr[i]);
    }
}

Upvotes: 0

Shamim Hafiz - MSFT
Shamim Hafiz - MSFT

Reputation: 22074

If you are strictly using C, you can't declare variables inside the For loop body as you are doing now. I have tried this using GCC and i got compile error.

error: 'for' loop initial declaration used outside C99 mode

Also, you seem to be re-declaring i, and some compilers won't able to detect that as a new scope.

Upvotes: 1

Mat
Mat

Reputation: 206689

for (int i = 0; ...

This syntax is C99, it is not allowed in previous standards. Since you've already declared i, you can just change that to:

for (i = 0; ...

If you want a block-level i in there anyway (it will shadow the i that you defined earlier in your function), then use:

int i;
for (i = 0; ...

or get a compiler that supports C99.

Upvotes: 4

Luchian Grigore
Luchian Grigore

Reputation: 258568

This code should compile, unless you actually forgot to close your function with a trailing }.

One other issue could be the redeclaration of i. I've seen this on some compilers. Also, a note - in the for loop you don't need to redeclare i, you can use the existing declaration.

I'm also assuming you defined the function print yourself.

Upvotes: 1

Related Questions