-4

How I will pass a 2D array in a function. I take input from keyboard but when I pass it into function it doesn't work.

for example

#include<bits/stdc++.h>

using namespace std;

void printGrid(int M, int N, int arr[][N])
{
    for(int i=0; i<M; i++)
    {
        for(int j=0; j<N; j++)
        {
            printf("%d ",arr[i][j]);
        }
        printf("\n");
    }
}

int main()
{
    int M,N;
    scanf("%d %d",&M,&N);
    int arr[M][N];
    printGrid(M,N,arr);
    return 0;
}

This solution doesn't work. It says N was undeclared on this scope.

Is there any way to work with 2D array in Function ?

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
  • Sequence of arguments are wrong `print(arr, M, N);` --> `print(M, N, arr);` – Gaurav Pathak Oct 17 '17 at 11:36
  • What you're doing is not valid C++. Are you *really* programming in C++? That's the only way to get that error. If so, then remove the C tag. And learn about [`std::vector`](http://en.cppreference.com/w/cpp/container/vector). – Some programmer dude Oct 17 '17 at 11:37
  • print(M, N, arr); still doesn't work – madmaxhasan Oct 17 '17 at 11:37
  • you are declaring `N` only after using it... – Leonardo Alves Machado Oct 17 '17 at 11:37
  • 2
    do you actually **have** a `main()` function? Show a [mcve] please. –  Oct 17 '17 at 11:38
  • i just need a solution of work with 2D array in Function.. – madmaxhasan Oct 17 '17 at 11:40
  • `int M, N` can be anything when compiled (and set through input) most compilers require a set size for an array unless you are using dynamic memory. If you want another option besides dynamic memory you should look into `std::array` or `std::vector` – AresCaelum Oct 17 '17 at 11:41
  • 4
    Are you using C or C++? This code looks like C and the array is illegal in C++. – NathanOliver Oct 17 '17 at 11:41
  • @MadMaxHaSaN this code won't work in C++. – Gaurav Pathak Oct 17 '17 at 11:43
  • 3
    Possible duplicate of [" 'X' not declared in this scope " error](https://stackoverflow.com/questions/21953684/x-not-declared-in-this-scope-error) – Gaurav Pathak Oct 17 '17 at 11:45
  • @Someprogrammerdude disagree with your edit here, the sensible assumption is that OP just doesn't know / didn't bother about the difference between C and C++. Code shown is clearly C. –  Oct 17 '17 at 11:46
  • 2
    [Variable-length arrays](https://en.wikipedia.org/wiki/Variable-length_array) does not exist in C++. Use [`std::vector`](http://en.cppreference.com/w/cpp/container/vector) instead (as already mentioned). – Some programmer dude Oct 17 '17 at 11:47
  • @FelixPalmen The invalid answer from the OP clearly shows this is C++, and logically the error is only possible when attempting to compile it with a C++ compiler. – Some programmer dude Oct 17 '17 at 11:47
  • 1
    @Someprogrammerdude no, it just shows he confuses the languages. Nobody would ever write this code "as C++". –  Oct 17 '17 at 11:48
  • 2
    @FelixPalmen Beginners, or people to fond of copy-pasta programming, do it all the time. And since a few major compilers *do* allow VLA's as an extension, it's probably works most of the time, leading to more confusion when it *stops* working. While some extensions (pragmas and `__attribute__` for example) might be good and added in a compatible way, this VLA extension is a really bad one IMO. – Some programmer dude Oct 17 '17 at 11:48
  • 1
    And as I posted on your now deleted answer, you *really* should read [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) – Some programmer dude Oct 17 '17 at 11:52

3 Answers3

3

You are compiling your code the wrong way. int arr[][N] is a variable-length array (VLA). This is a feature that was introduced in the C language in the year 1999. If you use a compiler which is older than that, or if you use a C++ compiler, you will get the error described.

Make sure to compile your code with a standard-compliant C compiler! You need to use a compiler which supports the C99 standard or later. For example you could use the GCC compiler version 5.x. Or if you use an older version of that compiler, set the compiler option gcc -std=c99.

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • any solution of c++ 17 compiler – madmaxhasan Oct 17 '17 at 11:49
  • @MadMaxHaSaN **if** this is supposed to be C++, then write C++. What you show here **is C code**. With C++, use containers from the STL, like e.g. already mentioned, the `vector`. –  Oct 17 '17 at 11:50
  • 4
    @MadMaxHaSaN You tagged this C so I gave a C answer. Thanks for wasting everyone's time. – Lundin Oct 17 '17 at 11:50
0

Why don't you try something like this:

#include <iostream>
#include <vector>

using namespace std;

void print(const vector<vector<int>>& arr) {
    for(auto& subArr : arr) {
        for(auto& element : subArr) {
            cout << element << ' ';
        }
        cout << '\n';
    }
}

int main() {

    int M,N;
    cin >> M >> N;
    vector<vector<int>> arr(M, vector<int>(N));
    print(arr);
}

This piece of code doesn't do much - it just initializes the 2D array to zeroes and prints it - but that's not the point. It shows some techniques that you should use if you're coding using modern C++. Besides it solves your problem of printing a 2D array in this case implemented as a vector of vectors.

I would recommend reading about std::vector: cppreference.com - std::vector

navyblue
  • 776
  • 4
  • 8
-2

Solution of my Problem... Thanks for you guys response..

#include<stdio.h>
#define MAX 500

int arr[MAX][MAX];

void printGrid(int M, int N)
{
    for(int i=0; i<M; i++)
    {
        for(int j=0; j<N; j++)
        {
            printf("%d ",arr[i][j]);
        }
        printf("\n");
    }
}

int main()
{
    int M,N;
    scanf("%d %d",&M,&N);
    printGrid(M,N);
    return 0;
}

this code work the limitation you give for the array

  • Well, it's some kind of a workaround. However, allocating a global array of an arbitrary size (and counting on the user that they don't feed the program with too big input) doesn't comply with any good programming practices. – navyblue Oct 17 '17 at 12:16