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]
    }
}
Smash
  • 3,722
  • 4
  • 34
  • 54
  • 1
    Avoid 2D arrays. It is much simpler to think in terms of 1D array. – Basile Starynkevitch Jan 31 '14 at 19:12
  • 2
    assume I can't for now :P – Smash Jan 31 '14 at 19:13
  • 1
    For 1-D: `int(*p)[10];` , for 2-D: `int(*p)[r][c]` – Grijesh Chauhan Jan 31 '14 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. – John Kugelman Jan 31 '14 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. – IdeaHat Jan 31 '14 at 19:37
  • @MadScienceDreams There are no "weird characteristics" when passing arrays by value and reference. Other than using more scalable containers (e.g. `std::vector`), there is no real reason to avoid using arrays. – Zac Howland Jan 31 '14 at 19:40
  • I mostly use 1D arrays (faster than vectors + continuous memory compared to 2D arrays) but in this case the variables I am referring too are used throughout the code I am too lazy to change them all to 1D – Smash Jan 31 '14 at 19:49
  • @ZacHowloand wierd characteristics: `typedef int int4[4]; void foo(int4 b) {std::cout << sizeof(b);/*return 8, pointer type*/} int main(int argc,char** argv){int4 val; std::cout << sizeof(val);/*returns 16, 4*sizeof(int)*/}` I've got no problem using arrays, I have problem using the C++ 2D builtin types. – IdeaHat Jan 31 '14 at 19:57

4 Answers4

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];
Smash
  • 3,722
  • 4
  • 34
  • 54
  • I was going to post an answer, but yours covers most of it. An example can be found [here](http://ideone.com/UWJRZu). – Zac Howland Jan 31 '14 at 19:41
  • Weird, I still have no idea why `double (&B)[1000][1000] = A;` doesn't work :-P – IdeaHat Jan 31 '14 at 19:45
  • + for self-learning, Btw, you could also so: `double **B = A;` then `double a = B[i][j];` that looks simpler. – Grijesh Chauhan Jan 31 '14 at 19:48
  • 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 **"` – Smash Jan 31 '14 at 19:52
  • 1
    @GrijeshChauhan no, they would not work. That's for jagged arrays only, doing that with a C++ 2D array would result in a segfault – IdeaHat Jan 31 '14 at 19:54
  • it will [work in C](http://codepad.org/dlpaXbKD) but @MadScienceDreams is correct in [C++ it is an error](http://codepad.org/S2WWag88). – Grijesh Chauhan Jan 31 '14 at 20:00
  • @GrijeshChauhan Thats because the compiler is optomizing out your unused variable, if you add a `printf("%f",(float)i);` to the end you'll get your seg-fault in C. – IdeaHat Jan 31 '14 at 20:11
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.

Community
  • 1
  • 1
kuroi neko
  • 8,479
  • 1
  • 19
  • 43
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).

IdeaHat
  • 7,641
  • 1
  • 22
  • 53
  • Your answer seems promising, but I get: `error C2440: 'initializing' : cannot convert from 'double (*)[1000]' to 'double (&)[1000][1000]'` – Smash Jan 31 '14 at 19:26
  • @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.... – IdeaHat Jan 31 '14 at 19:32
  • 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 Jan 31 '14 at 19:34
  • @Smash double[1000][1000] A is invalid syntax.... – IdeaHat Jan 31 '14 at 19:39
  • hmmm... sorry I meant `double A[1000][1000];` ... – Smash Jan 31 '14 at 19:40
  • You must be passing it into a function, then? – IdeaHat Jan 31 '14 at 19:41
  • not in this case, since it's a member variable it's readily accessible from the member function – Smash Jan 31 '14 at 19:42
  • anyways, thanks to your answer and Grijesh's comment I managed to answer my own question, ill upvote your answer if you get it to compile in MVS2010 ;P – Smash Jan 31 '14 at 19:45
-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];
aria nikan
  • 11
  • 1
  • 4