0

Excuse me if the title is misleading.

I'm attaching below some parts of my code which I think would be relevant to understanding the errors that the compiler flagged.

#include <stdio.h>

size_t count_gen(const size_t rowbound,const size_t colbound,const int num,double input[][rowbound][colbound],double output[][colbound])
{
    size_t i,j,row;
    for(j = 0; j < colbound; ++j)                                
    {
        for(i = 0; i < rowbound; ++i)                          
        {
            for(row = 0; row < rowbound; ++row)               
            {
                if(row == i)                                         
                    continue;
                else if(input[num][i][j] >= input[num][row][j])     
                    output[i][j]+=1;
            }
        }
    }
}

...

int main(void)
{
    size_t i,j,k,m,n;
    printf("Enter the number of parameters: ");
    scanf("%zu",&m);
    printf("Enter the number of objects: ");
    scanf("%zu",&n);
double SVNSFSS[4][n][m];
size_t counts1[n][m] = {{0}};
...
count_gen(n,m,0,SVNSFSS,counts1);       // 1st call to the function "count_gen"
...
return 0;
}

The errors:

5   99  C:\Users\dell pc\OneDrive\Documents\C programs\SVNSFSS_project.cpp  [Error] use of parameter outside function body before ']' token
5   109 C:\Users\dell pc\OneDrive\Documents\C programs\SVNSFSS_project.cpp  [Error] use of parameter outside function body before ']' token
5   110 C:\Users\dell pc\OneDrive\Documents\C programs\SVNSFSS_project.cpp  [Error] expected ')' before ',' token
5   111 C:\Users\dell pc\OneDrive\Documents\C programs\SVNSFSS_project.cpp  [Error] expected unqualified-id before 'double'

So, I got the errors in line 3 (in accordance with the code above), and it marked the second dimension namely rowbound of the function argument double input[][rowbound][colbound] as erroneous.

I am sort of clueless on how to resolve the issue! Please suggest what I should do.

2 Answers2

0

It looks like you are compiling this code as C++, not C (going by the .cpp filename suffix).

C++ does not support variable-length arrays, where the array size can be specified with a runtime variable or function parameter; you must use a constant expression for your array sizes. Simply declaring the parameters as const does not make them constant expressions.

Make sure you are compiling this code as C, not C++, and you shouldn't get that particular error anymore.

John Bode
  • 119,563
  • 19
  • 122
  • 198
  • Yes, sir... It got fixed. Please don't get angry at what I am about to ask. I got a new error saying: `[Error] variable-sized object may not be initialized` where I declared and initialized `size_t counts1[n][m]={{0}};`. Any suggestions regarding this? – Usual_Learner Jul 18 '22 at 15:54
  • I think if I use loops to initialize the array `counts1[n][m]`, it might work! – Usual_Learner Jul 18 '22 at 16:04
  • @Usual_Learner: Yeah, you can't use an initializer with variable-length arrays; you'll have to write a loop to assign each element independently. – John Bode Jul 18 '22 at 16:06
0

The error you posted, [Error] use of parameter outside function body before ']' token, is telling you exactly what is wrong. You can't use the parameter to the function in a variable-length array declaration in C++. You can, however, do this in C. See this answer: https://stackoverflow.com/a/25552114/2391458

If you're trying to force/enforce a particular dimension size on the input and output arrays in your function signature and want to use C++, then remember that parameters declared with array type decay to pointer type.

See this answer: https://stackoverflow.com/a/2276409/2391458 for more information.

FCo
  • 485
  • 5
  • 17
  • I am dumb, sir. While saving the file I didn't realize that the default file extension was .cpp and not .c! Sorry and thanks for the links. These might help me in the future. – Usual_Learner Jul 18 '22 at 15:57