2

I have a portion of code that goes like this (assume all types are int):

for(int i = 0; i < h; ++i)
{
    for(int i = 0; i < h; ++i)
    {
        if(...)
        {
             a = B[i][j]
        }
        else
        {
             a = C[i][j]
        }
    }
}

I would like to write it so that I only check the if condition once. But how would I go in declaring the pointer to the 2D array adequately (variable D in the example below) ?

if(...)
{
    D = B;
}
else
{
    D = C;
}

for(int i = 0; i < h; ++i)
{
    for(int i = 0; i < h; ++i)
    {
        a = D[i][j]
    }
}
8
  • 1
    Avoid 2D arrays. It is much simpler to think in terms of 1D array. Commented Jan 31, 2014 at 19:12
  • 2
    assume I can't for now :P Commented Jan 31, 2014 at 19:13
  • 1
    For 1-D: int(*p)[10]; , for 2-D: int(*p)[r][c] Commented Jan 31, 2014 at 19:13
  • 3
    @BasileStarynkevitch What a strange suggestion. What's hard about 2D arrays? If a 2D array models the problem well, use a 2D array. Commented Jan 31, 2014 at 19:18
  • @JohnKugelman There is a big difference between a sematically 2D array (like a jagged array) and the C++ 2D array concept, for everything between memory layout and syntax. In general, the C++ 2D array syntax is annoying as hell and non-intuitive, is constant size, and give very little (if any) performance boost over jagged arrays. They have weird characteristics with passing by value and passing by reference...so its usually better to just avoid them, imho. Commented Jan 31, 2014 at 19:37

4 Answers 4

4

Combining @MadScienceDreams' answer and @Grijesh Chauhan comment, the following seems to work:

double A[1000][1000];
double (*B)[1000][1000] = &A;

And then to access a value:

double a = (*B)[i][j];
Sign up to request clarification or add additional context in comments.

7 Comments

I was going to post an answer, but yours covers most of it. An example can be found here.
Weird, I still have no idea why double (&B)[1000][1000] = A; doesn't work :-P
+ for self-learning, Btw, you could also so: double **B = A; then double a = B[i][j]; that looks simpler.
I had already thought of double** before posting, but I get this in VS --> Error: a value of type "double (*)[1000]" cannot be used to initialize an entity of type "double **"
@GrijeshChauhan no, they would not work. That's for jagged arrays only, doing that with a C++ 2D array would result in a segfault
|
0
  const int w = 10;
  const int h = 10;

  char b[w][h];
  char c[w][h];
  char (&d)[w][h] = b;

  assert(&b[0][0]==&d[0][0]);

(this is assuming your using a 2D array and not the (better, imho) jagged array).

8 Comments

Your answer seems promising, but I get: error C2440: 'initializing' : cannot convert from 'double (*)[1000]' to 'double (&)[1000][1000]'
@Smash hmmm...it compiles just fine under g++. How are you initializing your array? This looks like you are using a jagged array of static arrays....
Initial 2D array is a class member variable declared in the header, double[1000][1000] A. Then in a member function I tried to do double (&B)[1000][1000] = A;
@Smash double[1000][1000] A is invalid syntax....
hmmm... sorry I meant double A[1000][1000]; ...
|
0

You can declare your array any way you like,
as long as the function prototype matches the array declaration.

C++ should not even allow such mismatches, but for some reasons it does exactly the same stupid thing as C.

See this answer for more details.

Comments

-1

hi to all

however other in above give your answer but result code is below
int B[w][h];
int C[w][h];
int (&D)[w][h] = (any clause) ? B : C;
for(int i =0;i < w;i++)
for(int j =0;j < h;j++)
a = D[i][j];

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.