4

I have written the following code but it is showing the error

use of parameter outside function body before ‘]’ token

The code is

#include <iostream>
using namespace std;
int n=10;
void a(int s[n][n])
{
    cout<<"1";
}
int main()
{
    int s[n][n]={0};
    a(s);
}

I am trying to pass a multidimensional array of variable size using global variable. I don't want to use vectors in this.

shivank01
  • 1,015
  • 3
  • 16
  • 35

3 Answers3

3

Firstly C++ doesn't have variable-length arrays, So Instead of int s[n][n]={0}; you should use std::vector<std::vector<int>> s(10,std::vector<int>(10));

Secondly how to pas 2D array to a function,

void a(std::vector<int> **s,int rows, int cols){
        cout<<"1";
        /* stuff with 2D array */
}
Achal
  • 11,821
  • 2
  • 15
  • 37
  • By variable length I mean the size of the array is given by the user.I don't mean changing the size of array.Secondly,I don't want to use vector in this. – shivank01 Feb 27 '18 at 16:54
  • 2
    `std::vector **s` are you suggesting to pass 2D array of vectors? – Killzone Kid Feb 27 '18 at 17:07
1

You've already received answers which explain the why. I'm offering this only as a matter of completeness to C++. Personally, though I don't understand why you're avoiding vectors, they do offer a more intuitive or pleasing solution. Inside of your function for handling the vectors, you can always consult std::vector<>.size() to ensure you stay within bounds or std::vector<>.at() and catch the exception that is thrown when accessing out of bounds. Nevertheless, your particular question may also be solved by templates. Below is your code, slightly modified, with comments to illustrate. I tested using gcc 4.8.5:

#include <iostream>
using namespace std;

// Made constant so that the compiler will not complain
// that a non-constant value, at compile time, is being
// used to specify array size.
const int n=10;

// Function template.  Please note, however, this template
// will only auto-gen functions for 2D arrays.
template<int lb>
void a(int s[][lb])
{
    // output the last element to know we're doing this correctly
    // also note that the use of 9 hard-codes this function template
    // to 2D arrays where the first dimension is always, at least, 10
    // elements long!!!!!!
    cout << s[9][lb - 1] << endl;
}

int main()
{
    int s[n][1];
    s[9][0] = 15;
    a<1>(s); // explicitly call template with the size of the last dimension
    a(s); // Call the same function generated from the previous call

    int t[n][2];
    t[9][1] = 17;
    a(t); // compiler implicitly determines the type of function to generate
}
Andrew Falanga
  • 2,274
  • 4
  • 26
  • 51
0

You can't. Your function a() needs to know the last dimension, which is the length of each row in the matrix. You need to pass this as an extra parameter to your function.

void a(int * matrix, int rows, int columns) {
   int row = ...
   int column = ...
   if (row < rows && column < columns) {
      cout << matrix[row*columns + column];
   }
}

int main() {
   ...
   a(&s[0][0], 10);
   ...
A Fog
  • 4,360
  • 1
  • 30
  • 32