-1

I have a function signature that uses a parameter value to dynamically allocate an array of the correct size

void print(int rows, int cols, char arr[rows][cols])

I am currently working in the Visual Studio IDE. It seems to have a problem with not knowing what the parameter is and therefore not knowing how much memory to allocate. It is a syntax error and won't even compile. Is there a way to ignore the error/compiler warning. I previously had a scanf compiler warning but disabled it following this link. Can I define something like

    #define _CRT_SECURE_NO_DEPRECATE

for this problem? Thanks

Community
  • 1
  • 1
  • 2
    just show the compiler output for better description – andrgolubev Feb 10 '17 at 06:25
  • 3
    Are you programming C or C++? It really makes a *big* difference since what you show for the function declaration is allowed in one language but not the other. Don't add unrelated language tags. – Some programmer dude Feb 10 '17 at 06:27
  • This doesn't look like valid C++. I think the third paramater must be a `char **arr`? Anyway, like @andrgolubev already said : Whats the compiler output? – AquilaRapax Feb 10 '17 at 06:31

2 Answers2

2
void print(int rows, int cols, char arr[rows][cols])

is not a valid declaration in C++. When a 2D array is passed, the value of the second dimension, cols in your case, must be known at compile time. The value of the first dimension may be compile time constant or be nothing but it can't be rows.

Valid declarations:

#define MAXCOLS 20
void print(int rows, int cols, char arr[][MAXCOLS])

and

#define MAXROWS 10
#define MAXCOLS 20
void print(int rows, int cols, char arr[MAXROWS][MAXCOLS])

and

#define MAXCOLS 20
void print(int rows, int cols, char (*arr)[MAXCOLS])
R Sahu
  • 196,807
  • 13
  • 136
  • 247
1

At least in C++, you can't declare an array parameter using dimensions specified in other parameters, like you are attempting.

A 1-dimensional fixed array (T[dim]) can decay into a pointer (T*), so it is possible to do something like this:

void print(int rows, char *arr);

...

char arr[5];
print(5, arr);

However, when passing a multi-dimensional array, all but the first dimension must be explicitly specified, eg:

void print(int rows, int cols, char (*arr)[10])

...

char arr[5][10];
print(5, 10, arr);

Which does not help in your situation if you don't know how large the array is actually going to be (presumably that is why cols is being passed in to begin with).

However, this can be solved using a template instead:

template<const int rows, const int cols>
void print(char arr[rows][cols])

...

char arr[5][10];
print<5, 10>(arr);
Remy Lebeau
  • 454,445
  • 28
  • 366
  • 620
  • 4
    That's a really bad and *incorrect* solution. A pointer to pointer is *not* equivalent to an array of arrays. An array of arrays is equivalent to a pointer to an array though, so for `char[5][10]` its equivalent type would be `char (*)[10]`. Also, the declaration shown by the OP *is* valid in C, which allows variable-length arrays. And we still don't know if the OP is programming in C or C++. – Some programmer dude Feb 10 '17 at 06:35
  • @Someprogrammerdude The user has a gold badge for c, I am certain this is just an honest mistake. – RoadRunner Feb 10 '17 at 07:47