4

I'm trying to pass a 2D array from a function to another function. However, the size of the array is not constant. The size is determined by the user.

I've tried to research this, but haven't had much luck. Most code and explanations are for a constant size of the array.

In my function A I declare the variable and then I manipulate it a bit, and then it must be passed to Function B.

void A()
{
      int n;
      cout << "What is the size?: ";
      cin >> n;

      int Arr[n-1][n];

      //Arr gets manipulated here

      B(n, Arr);
}

void B(int n, int Arr[][])
{
    //printing out Arr and other things

}
Maroun
  • 94,125
  • 30
  • 188
  • 241
Eric
  • 2,008
  • 3
  • 18
  • 31

3 Answers3

11

Use std::vector if you want dynamically-sized arrays:

std::vector<std::vector<int>> Arr(n, std::vector<int>(n - 1));
B(Arr);

void B(std::vector<std::vector<int>> const& Arr) { … }
  • While this is true, this doesn't answer his question. – Maroun Oct 28 '13 at 07:37
  • 2
    @MarounMaroun, sure it does. Hell, it goes further and is going to spare OP some real serious troubles later. – Griwes Oct 28 '13 at 08:10
  • 3
    OP asks how to create arrays with dynamic sizes, and I show him how to do this. `std::vector` is an abstraction that implements the concept _dynamic array_ and you should use it when you need a _dynamic array_. –  Oct 28 '13 at 08:13
  • 13
    `std::vector` is the basics. `new[]` is an advanced feature of C++ and only used when implementing low-level data structures. –  Oct 28 '13 at 08:17
  • @rightfold Basic for you, not for others. In the university I learn arrays in order to implement `vector` myself. – Maroun Oct 28 '13 at 08:20
  • @MarounMaroun You've provided _your_ answer, let others post theirs. Please refrain from shouting. – sehe Oct 28 '13 at 08:20
  • 1
    @MarounMaroun, universities are almost always wrong when it comes to programming. "The uni does that" is a bullshit argument. – Griwes Oct 28 '13 at 08:22
  • 6
    @MarounMaroun Bjarne Stroustrup (creator of C++) has book for novices: ["Programming -- Principles and Practice Using C++"](http://www.stroustrup.com/Programming/). And do you know what? `vector` is at page ~100, and `new` is only at the middle of the book - at page ~500 . – Evgeny Panasyuk Oct 28 '13 at 08:24
  • @EvgenyPanasyuk I'm not saying it's advanced topic, I'm saying that it's a very.. very good practice to work with arrays and try to enlarge and smaller them, for a practice.. I once had an exercise of implementing `vector` using arrays. This helped me a lot. – Maroun Oct 28 '13 at 08:25
  • 3
    Stroustrup: "By now, C++ has features that allow a programmer to refrain from using the most troublesome C features. For example, standard library containers such as vector, list, map, and string can be used to avoid most tricky low-level pointer manipulation." http://www.stroustrup.com/bs_faq.html – sehe Oct 28 '13 at 08:26
  • 5
    @MarounMaroun, no, it made you a stubborn guy who thinks that every programmer needs to learn manual memory management before sane and *modern* techniques. Eh. – Griwes Oct 28 '13 at 08:27
  • 3
    And **"Fundamentally, if you understand vector, you understand C++"** ([_Bjarne Stroustrup - The Essence of C++: With Examples in C++84, C++98, C++11, and C++14_](http://channel9.msdn.com/Events/GoingNative/2013/Opening-Keynote-Bjarne-Stroustrup)) – sehe Oct 28 '13 at 08:28
  • 3
    Please let's just stick to the technical points and if you want to discuss things on a tangent use [chat](http://chat.stackoverflow.com/rooms/10/loungec) not comments. – Flexo Oct 28 '13 at 09:12
3

Array size needs to be constant. Alternatively, you can use std::vector<std::vector<int>> to denote a dynamic 2D array.

Pai
  • 282
  • 2
  • 3
3

C++ does not support variable length arrays. Having C99 and compile it C only, you may pass the array like this:

#include <stdio.h>

void B(int rows, int columns, int Arr[rows][columns]) {
    printf("rows: %d, columns: %d\n", rows, columns);
}

void A() {
    int n = 3;
    int Arr[n-1][n];
    B(n-1, n, Arr);
}


int main()
{
    A();
    return 0;
}

Note: Putting extern "C" { } around the functions does not resolve the C++ incompatibility to C99:

  g++ (Ubuntu/Linaro 4.7.2-2ubuntu1) 4.7.2:
  error: use of parameter ‘rows’ outside function body
  error: use of parameter ‘columns’ outside function body
  warning: ISO C++ forbids variable length array
  • Dangnabit. This answers a question I had, but I don't like the answer. I was trying to get some perfectly fine C code to work in C++, and kept getting errors about type conversions. (It would have been nice if clang just said straight out that C++ does not support variable sized arrays.) I tried a bunch of variations with * and &, all to no avail. Now I know why. Isn't it against some sort of basic religious tenet for C to be able to do something that C++ cannot? – Mark Adler Aug 02 '15 at 19:02