Re: a pointer problem: can I use an array name as a pointer to it?
On Jun 28, 12:12 pm, Marcin Rodzik <marteno_ro...@o2.pl> wrote:
The best answer is to stop using raw arrays and learn to use
std::vector.
Thanks for your advice, however, I still would like to know how
to return a pointer/reference to a 2D array...
As Francis said in his reply, only the top level of an array decays
to a pointer, so you might want to implement your getArray function
as something like:
B * getArray() { return &array[0][0]; }
Then, in order to use this pointer in code that calls getArray to
access elements of the array, you will need to think about how C++
stores multi-dimensional arrays.
If you think of a 2-d array in terms of rows and columns, you
might visualize, say:
int array[3][5]
something like this:
cols
00 01 02 03 04
rows 10 11 12 13 14
20 21 22 23 24
However, if my understanding is correct, C++ will store this as
a contiguous series of ints as follows:
(row 0) (row 1) (row 2)
| 00 01 02 03 04 | 10 11 12 13 14 | 20 21 22 23 24 |
^ ^ ^
| | |
-----------------------------------
|
(col 0)
Given a pointer to the first element, which in this example is had
from:
int *p_arr = &array[0][0];
you can then access elements using the equivalence:
array[2][3] == p_arr[2 * 5 + 3];
or, more generally, for
int array[ROW_SIZE][COL_SIZE];
array[i][j] == p_arr[i * COL_SIZE + j];
// 0 <= i && i < ROW_SIZE; 0 <= j && j < COL_SIZE
To attempt to apply this to your specific example - for which you
only supply snippets, so much of this is necessarily filled in by
guesswork - you might think in terms of something like the
following:
#include <iostream>
const int ROW_SIZE = 16;
const int COL_SIZE = 16;
class B {
public:
int u;
B(int u = 0): u(u) { }
};
class A {
private:
B array[ROW_SIZE][COL_SIZE];
public:
A() {
for (int i = 0; i < ROW_SIZE; ++i)
for (int j = 0; j < COL_SIZE; ++j)
array[i][j] = B(i + j);
}
B * getArray() { return &array[0][0]; }
};
int main()
{
A a;
B *b = a.getArray();
int i = 2; // 0 <= i && i < ROW_SIZE
int j = 5; // 0 <= j && j < COL_SIZE
std::cout << "array[i][j].u == "
<< b[i * COL_SIZE + j].u << '\n';
return 0;
}
However, again like Francis, I would ordinarily avoid the
complications of working with multi-dimensional arrays in this
way in favour of std::vector, replacing your
B array[16][16];
with something like:
std::vector<std::vector<B> > array(16, std::vector<B>(16));
Because of this I have little hands-on experience of working with
arrays in the way you are attempting, so I should say that my
suggestion above _requires_ that C++ _always_ implements a 2-
dimensional array using contiguous storage that allows it to
be accessed "as if" it were a single-dimensional array using
the syntax:
array_ptr[row * COL_SIZE + col]
above. For this to work in an implementation-independent way
(as it works using gcc-4.3.2), perhaps someone could clarify
whether this is indeed guaranteed by the standard?
regards
Paul Bibbings
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]