简体   繁体   English

错误:ISO C 禁止嵌套函数 - 有什么问题?

[英]error: ISO C forbids nested functions - What's wrong?

What's wrong with this merge sort usage?这种合并排序用法有什么问题?
I'm trying to sort an array of size 9 as given in main().我正在尝试对 main() 中给出的大小为 9 的数组进行排序。
However, I'm using the merge_sort method and it gives me the error: "ISO C forbids nested functions" for every function.但是,我使用的是 merge_sort 方法,它给了我错误:每个函数的“ISO C 禁止嵌套函数”。
When I delete the arraycpy function these messages will no longer appear, so it's obviously a problem with "arraycpy".当我删除 arraycpy 函数时,这些消息将不再出现,所以这显然是“arraycpy”的问题。
Note that I can't use the string.h library since I'm limited to only these 3.请注意,我不能使用 string.h 库,因为我仅限于这 3 个。

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

void merge(int a[], int na, int b[], int nb, int c[])
{
   int ia, ib, ic;
   for(ia = ib = ic = 0; (ia < na) && (ib < nb); ic++)
   {
       if(a[ia] < b[ib]) {
           c[ic] = a[ia];
           ia++;
       }
       else {
           c[ic] = b[ib];
           ib++;
       }
   }
   for(;ia < na; ia++, ic++) c[ic] = a[ia];
   for(;ib < nb; ib++, ic++) c[ic] = b[ib];
}

void arraycpy(int *dest, int *src, int n)
{
    int i;
    int *src_p, *dst_p;

    src_p = (int*)src;
    dst_p = (int*)dest;

    for (i = 0; i < n; i++) {
        *(dst_p+i) = *(src_p+i);
}

void internal_msort(int a[], int n, int helper_array[])
{
    int left = n/2;
    int right = n/2;
    if (n < 2)
        return;
    internal_msort(a, left, helper_array);
    internal_msort(a + left, right, helper_array);
    merge(a, left, a + left, right, helper_array);
    arraycpy(a, helper_array, n*sizeof(int));
}

void merge_sort(int a[], int n)
{
   int *tmp_array = malloc(sizeof(int) * n);
   internal_msort(a, n, tmp_array);
   free(tmp_array);
}

void rem_sort(int array[], int size)
{
    merge_sort(array, size);
}

int main()
{
    int array[] = {3,55,72,4,21,6,9,0,4};
    merge_sort(array, 9);
    for (int i=0;i<9;i++)
        printf("%d | ", array[i]);

  return 0;
}

You missed a closing braces for for loop in the function arraycpy 您错过了arraycpy函数中for循环的arraycpy

void arraycpy(int *dest, int *src, int n)
{
    int i;
    int *src_p, *dst_p;

    src_p = (int*)src;
    dst_p = (int*)dest;

    for (i = 0; i < n; i++) {
        *(dst_p+i) = *(src_p+i);
    }
 // ^^^
}

So, the next braces is being taken as the braces for the for loop, and thus when you define the next function, it is being defined inside the next function arraycpy 因此,下一个括号被用作for循环的括号,因此,当您定义下一个函数时,它被定义在下一个函数arraycpy

I had the same error that complained on my main file.我在主文件中遇到了同样的错误。 This file had not bracket imbalances.该文件没有括号不平衡。 This was in my opinion strange.这在我看来很奇怪。 But the error was produced by an other file that I had included in the main.c file.但是错误是由我包含在 main.c 文件中的另一个文件产生的。

Check edited #include files if you can not find the open "}"如果找不到打开的“}”,请检查编辑过的 #include 文件

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

相关问题 错误] ISO C++ 禁止指针和整数之间的比较 [-fpermissive] - Error] ISO C++ forbids comparison between pointer and integer [-fpermissive] ISO C ++禁止可变大小的数组(编译错误) - ISO C++ forbids variable-size array (compilation error) ISO C禁止在C中使用空的初始化程序括号 - ISO C forbids empty initializer braces in C ISO C禁止前向参数声明 - ISO C forbids forward parameter declaration 警告:ISO C禁止空翻译单元 - Warning: ISO C forbids an empty translation unit ISO C++ 禁止在 Arduino c 串行通信中比较指针和整数 [-fpermissive] 错误 - ISO C++ forbids comparison between pointer and integer [-fpermissive] error in Arduino c serial communication ISO C ++禁止比较指针和整数:C错误中的文件处理 - ISO C++ forbids comparison between pointer and integer : File handling in C error 调用scanf()会给出错误“ ISO C ++禁止将其强制转换为用作左值的非引用类型” - Calling scanf() gives error “ISO C++ forbids cast to non-reference type used as lvalue” ISO C ++禁止在devc ++中比较指针和整数[-fpermissive]错误 - ISO C++ forbids comparison between pointer and integer [-fpermissive] error in devc++ 将结构指针传递给函数时出错“ISO C禁止转发参数去除”? - Error “ISO C forbids forward parameter decleration” when passing a struct pointer to a function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM