-3

Let's say we have a 2D array: int array[2][3];
I know we can use a single pointer to represent this: int (*arrayPointer)[2][3];
Or we can also use a double pointer (a pointer to a pointer) with new keyword to create an euqivalent 2D array on the heap.
But, Is there a direct syntax using a double pointer (but without using new keyword) to represent the same array with fixed size on the stack?
Like how to make: "int * [...] * [...] 2Dpointer;" works and represents a 2D array[2][3] directly? Please help :)

Crackie
  • 203
  • 6
  • 1
    Do not mess around with raw pointers in c++. For what you want, there's `std::array` already. – πάντα ῥεῖ Aug 25 '23 at 12:57
  • 3
    Double pointers require an intermediary pointer array: first level points to an array of pointers which in turn point to the actual data. 2D arrays are different animals... – Serge Ballesta Aug 25 '23 at 12:58
  • 2
    Time for you to start reading the ["C++" core guidelines](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines) specialy R.11. Just do not work with raw pointers and pointer arithmetic unless you really have to. (Maybe just maybe inside some datastructure). For all other cases there are much better C++ alternatives (your logic still has its basis in "C" style coding). – Pepijn Kramer Aug 25 '23 at 13:00
  • And btw: There's also _placement new_, if you really need to manage that yourself. – πάντα ῥεῖ Aug 25 '23 at 13:01
  • 2
    The type you are looking for is `std::array,2>` and you can pass this by value (copy) or by (const) referenence to any function pretty much like any other C++ object. – Pepijn Kramer Aug 25 '23 at 13:01
  • Note for dynamic allocation on the heap you could use : `std::vector> values(2);` and again you end up with code that doesn't need any (visible) pointers. – Pepijn Kramer Aug 25 '23 at 13:04
  • I have read the duplicate (i know that already), but it seems not to solve my confusion here :D – Crackie Aug 25 '23 at 13:05
  • anyway thanks everyone – Crackie Aug 25 '23 at 13:06
  • 4
    Arrays are not pointers. Pointers are not arrays. Due to *pointer decay*, an array will implicitly convert to a pointer to the first element of the array. That can make arrays *seem* like pointers, but that's just convenience behavior inherited from C — arguably, an inconvenience feature in C++. But that's why C++ provides alternatives to C style arrays. – Eljay Aug 25 '23 at 13:11
  • 1
    @Eljay that is the exact answer i'm looking for! thank a lot :) – Crackie Aug 25 '23 at 13:13
  • 2
    The **name** of an array decays into a pointer to its first element (in most contexts). A pointer to an array is not the name of an array; a pointer to an array does not decay into a pointer to a pointer. – Pete Becker Aug 25 '23 at 13:39
  • Make a 1D array if `T`. Logically split it up by strides. Make a 1D array of `T*`. Point each element of the `T*` array at one stride in the array of `T`. No `new` required. – user4581301 Aug 25 '23 at 16:53

0 Answers0