Home > Back-end >  Error: use of parameter outside function body before ']' token (while passing a 3d array t
Error: use of parameter outside function body before ']' token (while passing a 3d array t

Time:07-19

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.

CodePudding user response:

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.

CodePudding user response:

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.

  • Related