0

I don't know why my Microsoft Visual C++ 2010 Express doesn't support a code like this:

void ar(int n,int m, short ar[n][m]);

The thing that happens is that the letter n and m gets undercovered in red and it says:

//Error: a parameter is not allowed.

I'm coding in C and currently trying to learn about pointers and arrays.

Mat
  • 202,337
  • 40
  • 393
  • 406
user2283719
  • 153
  • 2
  • 10

3 Answers3

0

Try this:

   void ar(int,int,short (*)[]);

if it's a declaration and

void ar(int m,int n,short (*ar)[]){} // Refer my Edit

it it's the definition of the function.

Edit Are you really using the same name for the function and the 2D array which you are trying to pass as an argument to the function?

Rüppell's Vulture
  • 3,583
  • 7
  • 35
  • 49
  • The thing i would like to produce is :void ar(int n,int m, short (*ar)[]) { int r; int c; for( r = 0; r < n; r++ ) for( c = 0; c < m; c++ ) total += (*ar)[r][c] } But, that doesnt work because i want to have [][] which doesnt work and I've tried it b4, maybe u know? – user2283719 May 05 '13 at 19:51
0

You cannot have the passed array size to depend on a variable (moreover, passed in the same signature). It compiles in Clang, but it's not legal C89, it is C99 and AFAIK Microsoft VC does not fully support that.

Be careful: you are calling the function and its parameter by the same name and this might lead to funny, obscure errors. Either rename the function or the last parameter.

EDIT: try compiling this:

void ar(int n,int m, short ars[n][m])
{
}

int main()
{
    short a[1][2] = {{22,22}};
    ar(1,2,a);
}

it goes fine across all compilers installed on my system, so if it doesn't compile, either you have non-standard, exoteric settings enabled in your IDE or you are in strong need of a decent compiler.

Stefano Sanfilippo
  • 32,265
  • 7
  • 79
  • 80
  • That means: immediately rename the parameter to something else. – Stefano Sanfilippo May 05 '13 at 19:40
  • When i write it like that: void ar(int n,int m, short ar[][]); it compiles as the first bracket and last bracket is an array an the two ][ are like the number that the compiler thought i declared. Then it doesnt work either.Error. An array may not have elements of this type – user2283719 May 05 '13 at 19:45
  • Then try `void ar(int n,int m, short **ar)`. If this works, get a more conformant compiler, such as Clang, Intel or GCC. – Stefano Sanfilippo May 05 '13 at 19:49
  • I've also tried it b4 posting because i know that i want to use 2 arrays, but it doesn't work, i thought it would but not for me ;S – user2283719 May 05 '13 at 19:53
  • **Do rename the third parameter**. What you said above is incorrect: `][` is not a number and you cannot trick the compiler into thinking that it is. – Stefano Sanfilippo May 05 '13 at 19:58
0

VC (C89) can't write like your code.

for VC like this

#include <stdio.h>

void ar(int n,int m, short *a){
    int i,j, total=0;
    for(i=0;i<n;++i)
        for(j=0;j<m;++j)
            total += a[i*m+j];
    printf("total=%d\n", total);
}
int main(void){
    short a[2][3] = {{1,2,3},{4,5,6}};
    ar(2, 3, a);// a -> &a[0][0]
    return 0;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70