简体   繁体   中英

Missing ; before 'type' error

This is part of my code, do you know what causes this missing ; before 'type' error? It disappears when I delete sort(arr, size) in the main() function...

#include <stdio.h>

    void sort(int*, int);

    int main() {
        int arr[] = {5, 1, 3, 0, 9};
        int size = sizeof(arr) / sizeof(arr[0]);

        sort(arr, size);

        int i;
        for(i = 0; i < size; i++)
            printf("%d", arr[i]);

        getchar();
        return 0;
    }

    void sort(int *array, int size) {
        // ...
    }

You are most likely using a compiler that is strictly C89 and thus does not allow you to define a variable after non-definition code in the same block. Move the int i; above the sort call and it should work again:

int main() {
    int arr[] = {5, 1, 3, 0, 9};
    int size = sizeof(arr) / sizeof(arr[0]);
    int i;

    sort(arr, size);

    for(i = 0; i < size; i++)
        printf("%d", arr[i]);

    getchar();
    return 0;
}

Or even better, consider using the C99 standard if your compiler supports it. This would even to allow you to inline the int i , ie like this: for(int i = 0; i < size; i++)

If you're in proper old school C you can't define a variable after a function call.

So just do:

#include <stdio.h>

    void sort(int*, int);

    int main() {
        int arr[] = {5, 1, 3, 0, 9};
        int size = sizeof(arr) / sizeof(arr[0]);
        int i;

        sort(arr, size);


        for(i = 0; i < size; i++)
            printf("%d", arr[i]);

        getchar();
        return 0;
    }

    void sort(int *array, int size) {
        // ...
    }

You can't declare variables in midst of the code in pre-C99 C. Move your int i ; to the beginning of the block, near rest of variables declarations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM