简体   繁体   English

我无法运行交流程序

[英]I can not run a c program

I tried to run this c program in borland c++. 我试图在Borland C ++中运行此C程序。 It shows "expression required", while declaring the array int holes[size] . 在声明数组int holes[size]同时,它显示“ expression required”。 This is the only error that is shown. 这是显示的唯一错误。 I tried to solve it but it still shows the same problem. 我试图解决它,但它仍然显示相同的问题。

How can I solve this problem? 我怎么解决这个问题?

    /*
      *  C Program to Implement Pigeonhole Sort
      */
     #include <stdio.h>

     #define MAX 7

     void pigeonhole_sort(int, int, int *);
     void main()
     {
       int a[MAX], i, min, max;
       printf("enter the values into the matrix :");
       for (i = 0; i < MAX; i++)
       {
           scanf("%d", &a[i]);
       }
       min = a[0];
       max = a[0];
       for (i = 1; i < MAX; i++)
       {
           if (a[i] < min)
           {
               min = a[i];
           }
           if (a[i] > max)
           {
               max = a[i];
           }
       }
       pigeonhole_sort(min, max, a);
       printf("Sorted order is :\n");
       for (i = 0; i < MAX; i++)
       {
           printf("%d", a[i]);
       }
   }

   /* sorts the array using pigeonhole algorithm */
   void pigeonhole_sort(int mi, int ma, int * a)
   {

       int size, count = 0, i;
       int *current;
       current = a;
       size = ma - mi + 1;
       int holes[size];
       for (i = 0; i < size; i++)
       {
           holes[i] = 0;
       }
       for (i = 0; i < size; i++, current++)
       {
           holes[*current-mi] += 1;
       }
       for (count = 0, current = &a[0]; count < size; count++)
       {
           while (holes[count]--> 0)
           {
               *current++ = count + mi;
           }
       }          
    }

int holes[size] is a variable-length array (VLA), a feature that has "only" been around in C for 17 years and is not supported by C++. int holes[size]是可变长度数组(VLA),此功能在C语言中仅存在17年,而C ++不支持。

So it would seem that you either have a completely outdated compiler (Borland has not released any compilers for the past 10 years) or you are trying to compile C code with a C++ compiler. 因此,您似乎已经拥有完全过时的编译器(Borland在过去10年中未发布任何编译器),或者您正在尝试使用C ++编译器来编译C代码。 Neither will work. 两者都不起作用。


If by "Borland" you happen to mean Embarcadero C++ Builder, then you merely have to tell it to compile the code as C instead of C++. 如果用“ Borland”来形容Embarcadero C ++ Builder,那么您只需要告诉它将代码编译为C即可,而不是C ++。

Otherwise, you will have to upgrade to a modern C compiler, like for example GCC/Mingw. 否则,您将必须升级到现代C编译器,例如GCC / Mingw。 For example by downloading the completely free Windows version of the Codeblocks IDE, which comes with that compiler pre-installed. 例如,通过下载完全免费的Windows版本的Codeblocks IDE,它已预安装了该编译器。

在函数Pigeonhole_sort的第三个参数中,您声明一个指针,并在调用语句中传递一个数组

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

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