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

}
3
  • You pass it as a variable to the function, like you have done in the question. Commented Oct 28, 2013 at 7:27
  • 2
    @NickVeys: No, you don't, since this is not a valid way to create an array. Commented Oct 28, 2013 at 10:37
  • @LightnessRacesinOrbit Oops! I only looked at the int param. I guess being flippant bit me there. :) Commented Oct 29, 2013 at 16:19

3 Answers 3

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) { … }
Sign up to request clarification or add additional context in comments.

13 Comments

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.
std::vector is the basics. new[] is an advanced feature of C++ and only used when implementing low-level data structures.
@MarounMaroun Bjarne Stroustrup (creator of C++) has book for novices: "Programming -- Principles and Practice Using C++". And do you know what? vector is at page ~100, and new is only at the middle of the book - at page ~500 .
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." stroustrup.com/bs_faq.html
@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.
|
3

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

Comments

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

1 Comment

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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.