0
class Class
{
public:
    Class(array[3][3])    //the constructor
    {
      this->array = array
    }    

    array[3][3];
};    

int main()
{
    array[3][3] = {...initialization...};
    Class object(array[3][3]);
}

I want to make an object, which uses the 2d array and modifies it. I know that C arrays are just pointers to an address, but I couldn't pass it in the constructor no matter how many *, & or [] I write.

The most clever thing I could think of is making an array of POINTERS in the class, and assigning each pointer, to the address of the original array's element via for loop, but then every time I want to modify, or read from the array in main, I have to write for example *array[2][1] = 3.

Any clever solution?

TheFinalCutBG
  • 43
  • 1
  • 6
  • Your mistake is not passing the array into the constructor, your mistake is thinking that you can copy an array using `=`. That does not work, not matter how hard you try. Write a loop, or use `std::copy` or `memcpy`. – john Jul 04 '20 at 19:13
  • BTW arrays are arrays and pointers are pointers. It's incorrect to say that a C array is a pointer. A C array will convert to a pointer, but that's not the same thing at all. – john Jul 04 '20 at 19:15
  • Yep. Arrays array are an elegant solution to 1970s problems. There are a huge number of things you'd expect to be able to do with them you can't simply because doing it in the 1970s on a computer with practically no memory and a CPU clocked in the kHz would have been really, really dumb. – user4581301 Jul 04 '20 at 19:15
  • I don't think the suggested duplicate was good. Although they don't realise it the OP problem is copying the array, not passing it to the constructor. – john Jul 04 '20 at 19:21
  • Probably I haven't asked my question correctly. I want to assign the pointer of my array in the object, to point to the array in the main function, but I get an error. I don't want any copying or for loops or whatsoever. I just want a pointer which knows it is pointing to an array of size [3][3] and assign it to point to the address to which another pointer points... – TheFinalCutBG Jul 04 '20 at 19:23
  • @john, the title asks how to pass an array, the dupe answers this question. The code in the question has a lot of issues, so what the real problem is is not clear. – Evg Jul 04 '20 at 19:23
  • @Evg the op has now clarified the question, turns out they are asking something completely different. – john Jul 04 '20 at 19:24
  • Something like [this](https://godbolt.org/z/bGw8Uq)? – Evg Jul 04 '20 at 19:25
  • @Evg maybe although they explicitly ask for a pointer. – john Jul 04 '20 at 19:25
  • @john, that was the most clever thing. Maybe using a reference is even cleverer. ;) – Evg Jul 04 '20 at 19:27
  • Seems this is a duplicate after all https://stackoverflow.com/questions/8767166/passing-a-2d-array-to-a-c-function/17569578 – john Jul 04 '20 at 19:33

2 Answers2

4

If I finally got the question correctly, you can use a reference to an array:

struct Class {
    Class(int (&array)[3][3]) : array_(array)
    {}

    void set11(int value) {
        array_[1][1] = value;
    }

    int (&array_)[3][3];
};


int main() {
   int array[3][3]{};

   Class object(array);
   object.set11(99);

   std::cout << array[1][1];  // Prints 99
}

If that's not what you want, please clarify your question.

Evg
  • 20,870
  • 4
  • 34
  • 69
  • Lol, yes! That seem like it! Will try it as soon as I can, but even as I look at it - that's exactly what I want! I was missing the moment with the brackets around the array's reference :) – TheFinalCutBG Jul 04 '20 at 19:33
  • @TheFinalCutBG, see [The "Clockwise/Spiral Rule"](http://c-faq.com/decl/spiral.anderson.html). It is about C, but you can easily adapt it to C++ references, too. – Evg Jul 04 '20 at 19:35
  • 1
    Will do! And yes - I tried it and it works like a charm! – TheFinalCutBG Jul 04 '20 at 19:47
0

Here's how to declare a pointer in your class that can point to the array in main.

class Class
{
public:
    Class(int (*array)[3])
    {
         this->array = array;
    }
    int (*array)[3];
};


int main()
{
    int array[3][3] = { ... };
    Class object(array);
}
john
  • 71,156
  • 4
  • 49
  • 68