0

I am learning C ++, I'm beginner about C++. How can I change the array in the function(int fun(..)) if I declare the array in the main function like char arr [10][10].

#include <iostream>
using namespace std;
int func(....);
int main(){
   char arr[10][10];
   ...
   return 0;
}
anonim
  • 57
  • 5
  • 1
    Arrays in C++ are tricky. Can you be very specific about how you want this to behave? Be as precise as possible. Your question right now is very nebulous and a proper answer is something like "please learn how arrays work". – tenfour Oct 19 '20 at 09:14
  • 1
    Does this answer your question? [Passing a 2D array to a C++ function](https://stackoverflow.com/questions/8767166/passing-a-2d-array-to-a-c-function) – Yksisarvinen Oct 19 '20 at 09:14
  • 2
    In C++ you have multiple options. I would recommend that you start using [`std::array`](https://en.cppreference.com/w/cpp/container/array) since it would simplify many things. – Some programmer dude Oct 19 '20 at 09:15
  • This may also help (std::array): https://stackoverflow.com/questions/30263303/stdarray-vs-array-performance – Jose Oct 19 '20 at 09:15

2 Answers2

2

Let's get some things out first; using namespace std;, return 0; in main and C-style arrays are discouraged in modern C++. Rewriting the code to utilize recommended style yields this:

#include <iostream>
#include <array>

int func(....);

int main(){
   std::array<std::array<char, 10>, 10> arr;
   ...
}

The type is tedious to write, so we can make an alias:

template<typename T>
using TenByTenArray = std::array<std::array<T, 10>, 10>;

Now the declaration becomes more readable:

TenByTenArray<int> arr;

And so our function can now take a reference to that type easily:

int func(TenByTenArray<int> & arr) {
    // use arr to modify it
}

Making an example complete program:

#include <iostream>
#include <array>

template<typename T>
using TenByTenArray = std::array<std::array<T, 10>, 10>;

void func(TenByTenArray<int> & arr) {
    for (int x = 0; x < 10; x++) {
        for (int y = 0; y < 10; y++) {
            arr[x][y] = x + y*100;
        }
    }
}

int main(){
   TenByTenArray<int> arr;
   func(arr);
   std::cout << arr[4][2] << std::endl;
}
Bartek Banachewicz
  • 36,624
  • 6
  • 81
  • 129
  • 1
    Rather than `TenByTenArray`, I would use something like `Array2D`, that way it's one type that works for all sizes and not much more typing. One could probably even have one template that would allow arbitrary numbers of dimensions, although I imagine that would need some mind-boggling recursive traits struct, so maybe not the easiest to explain! – underscore_d Oct 19 '20 at 09:24
  • 1
    @underscore_d This is just for the example, the actual alias would depend on necessary use case. In case anyone reading the comments wants that, it's `template using Array2D = std::array, x>;` The N-dimensional variant is waaaay outside the scope of even the comment section here. – Bartek Banachewicz Oct 19 '20 at 09:26
  • 2
    "`using namespace std;`, `return 0;` in `main` and C-style arrays" -- which one of these is not like the others? Hint: just because you can omit `return 0;` doesn't mean you should. – Pete Becker Oct 19 '20 at 09:31
  • 1
    Yeah, I don't think it's good to recommend omitting `return 0`, which AFAICT has no good reason to be allowed other than to make lazy code valid... and which might risk making newbies think they can omit return values elsewhere and get the 'default' value anyway. – underscore_d Oct 19 '20 at 09:36
  • @underscore_d You'll get a fat warning elsewhere. I don't want to debate this here (again, off topic for this comment section) - I feel that in the short examples saving an extra line is worth it. I almost never use return codes because I rarely write command line tools. For all other use cases, the return codes are ignored, and you will signal the result to the user in some other way. In that sense, the default exit from the program is successful one and that sits "more right" with me than bending the rules on a function returning `int` (that is special in other ways anyway). – Bartek Banachewicz Oct 19 '20 at 09:52
1

Assuming you want to use C arrays, then the usual answer is

int func(char (*ptr)[10]);

Note that ptr is a pointer, it is impossible to pass a C array to a function in C++ (or in C). This is one very good reason not to use C arrays in a C++ program (there are others as well).

You can also pass an C array by reference in C++

int func(char (&ptr)[10][10]);

Unlike the pointer solution above this gives you no flexibility about the array dimensions, it must be 10 by 10.

Yet another possibility is to pass the array as part of a struct.

struct Array10by10
{
    char data[10][10];
};

int func(Array10by10 arr);

If all this seems a bit complicated, and since you are learning C++, you really should be learning the easier C++ approach, which is to use the class templates std::array or std::vector instead of C arrays.

john
  • 71,156
  • 4
  • 49
  • 68