简体   繁体   中英

I can not run a c program

I tried to run this c program in borland c++. It shows "expression required", while declaring the array int holes[size] . 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++.

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. 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++.

Otherwise, you will have to upgrade to a modern C compiler, like for example GCC/Mingw. For example by downloading the completely free Windows version of the Codeblocks IDE, which comes with that compiler pre-installed.

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

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