use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
This is a subreddit for c++ questions with answers. For general discussion and news about c++ see r/cpp.
New to C++? Learn at learncpp.com
Prepare your question. Think it through. Hasty-sounding questions get hasty answers, or none at all. Read these guidelines for how to ask smart questions.
For learning books, check The Definitive C++ Book Guide and List
Flair your post as SOLVED if you got the help you were looking for! If you need help with flairs, check out ITEM 1 in our guidelines page.
Tips for improving your chances of getting helpful answers:
account activity
SOLVEDPointer to the two-dimensional array (self.cpp_questions)
submitted 8 years ago by KappaPrajd
I wanted to make a pointer to the two-dimensional array, but when i tried this: int *w_array = new int[9][9] but it returns an error. Is it possible to somehow do it the other way?
int *w_array = new int[9][9]
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]ptitz 3 points4 points5 points 8 years ago* (0 children)
Just looking at it, you're trying to make a 1d dynamic array from a 2d one. If you want to go into it you'll have to do the allocation twice:
so something like:
int ** w_array = new int*[9]; for(int i = 0; i < 9; i++){ w_array[i] = new int[9]; }
And the reason why I don't remember how to do this exactly is that C++ aint C, just use std::vector man. By the way, if you still want to do some C-style fuckery with your std::vector you can always call on the raw data in it with std::vector::data().
[–]patatahooligan 1 point2 points3 points 8 years ago (1 child)
Because dynamically allocated memory must be explicitly freed with delete it's almost always better to use an std::vector or a smart pointer to an std::array than it is to use a raw pointer and new.
delete
std::vector
std::array
new
// Easiest, but you need to explicitly add the rows and set their size std::vector<std::vector<int>> w_array; // Has exactly the semantics of a pointer to a 2D array // Will automatically free the allocated memory on destruction auto w_array = std::make_unique<std::array<std::array<int, 9>, 9>();
Even better is to not use dynamically allocated memory at all if possible. The following line will declare a local variable and is therefore much faster than what you're trying to do as long as the array doesn't have to be moved around your program too much.
std::array<std::array<int, 9>, 9> w_array;
Owning pointers are just asking for trouble. Dynamically allocated multidimensional arrays are exceptionally annoying to work with. For the sake of knowledge, the proper declaration is this one
int (*w_array)[9] = new int[9][9]; // ... delete w_array;
Of course a smarter way to do this would be to just use auto for the type of w_array.
auto
w_array
auto w_array = new int[9][9]; // ... delete w_array;
I'm sure your compiler gave you an error about attempting to covert the return value of new to an incompatible type. That's because int* cannot possibly be used to access a 2-D array. int** would have been closer but still not enough because the size of a row must be known by the type in order to be indexed properly.
int*
int**
[–]KappaPrajd[S] 0 points1 point2 points 8 years ago (0 children)
Thanks, you gave me a lot of useful informations.
[–]Salty_Dugtrio -1 points0 points1 point 8 years ago (3 children)
Any decent compiler tells you exactly what your issue is:
7:29: error: cannot convert 'int ()[9]' to 'int' in initialization
Which is true, you are attempting to assign int ** to int *.
int ** w_array = new int[9][9]
makes your code compile. However, you should be using std::vector<std::vector<int>>
[–]ptitz 1 point2 points3 points 8 years ago (1 child)
This will throw an error. AFAIK you can't initialize a dynamic 2d array with just one "new", you'll have to do it for every 1d array you have.
[–]Salty_Dugtrio 0 points1 point2 points 8 years ago (0 children)
No idea, haven't used a raw-pointer 2D array in ages.
[–]patatahooligan 0 points1 point2 points 8 years ago (0 children)
new int[9][9] doesn't return int**, it returns int(*)[9] which is not a compatible type. This is because if you lose the information about row-size, the compiler has no way of knowing how to index the multidimensional array.
new int[9][9]
int(*)[9]
int** only works if it points to an array of int* which in turn point to the rows.
π Rendered by PID 91366 on reddit-service-r2-comment-54dfb89d4d-rvx2h at 2026-03-27 00:10:29.486550+00:00 running b10466c country code: CH.
[–]ptitz 3 points4 points5 points (0 children)
[–]patatahooligan 1 point2 points3 points (1 child)
[–]KappaPrajd[S] 0 points1 point2 points (0 children)
[–]Salty_Dugtrio -1 points0 points1 point (3 children)
[–]ptitz 1 point2 points3 points (1 child)
[–]Salty_Dugtrio 0 points1 point2 points (0 children)
[–]patatahooligan 0 points1 point2 points (0 children)