简体   繁体   English

失踪 ; 在“类型”错误之前

[英]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... 当我在main()函数中删除sort(arr,size)时,它消失了...

#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. 您最有可能使用严​​格为C89的编译器,因此不允许您在同一块中的未定义代码之后定义变量。 Move the int i; 移动int i; above the sort call and it should work again: 以上的sort调用,它应该可以再次工作:

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. 甚至更好的是,如果编译器支持,请考虑使用C99标准。 This would even to allow you to inline the int i , ie like this: for(int i = 0; i < size; i++) 这甚至允许您内联int i ,即像这样: 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. 如果您使用的是老式C语言,则无法在函数调用后定义变量。

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 ; 你不能在预C99 C的代码中间声明变量将您的int i ; to the beginning of the block, near rest of variables declarations. 到块的开始,靠近变量声明的其余部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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