0

Why can't i use the code below? I know that a matrix is defined like a one dimentional array follow by each other.

How can i make it possible?

All I need is optimization.

MyStructure* myStructure[8][8];
int i = 0;

for(MyStructure* s = myStructure[0][0]; i<64; i++,s++)
{

}
9
  • Never use Type* variable. Always use Type *variable. Mind the space. The * is part of the variable, not the type. Consider, int *foo, goo vs. int* foo, goo. Commented Jun 30, 2015 at 3:51
  • What exactly is the problem? Because this much code seems to work. I think we need more context here. Commented Jun 30, 2015 at 3:53
  • @user3427419: Not quite. And plenty of places prefer the Type* variable syntax Commented Jun 30, 2015 at 3:54
  • s is not an address of myStructure[0][0] (which would be of type MyStructure**) - it's the value of myStructure[0][0]. s++ moves to the next element in the array to which myStructure[0][0] points (and if it doesn't point to an array, then your program exhibits undefined behavior). In any case, the value of s++ bears no relation to myStructure[0][1] nor myStructure[1][0] Commented Jun 30, 2015 at 3:54
  • @user3427419 What of type * foo? Commented Jun 30, 2015 at 3:54

2 Answers 2

1

Since it is tougher to demonstrate this with pointers to objects, I've subbed in the common integer in place of the pointer to MyStructure. The levels of indirection are unchanged, and it is the level of indirection that matters to the OP's problem.

By the way, do not do this. Use Ediac's solution. I'm only trying to point out where things went wrong for the OP. Walking through a 2D array in one dimension MAY work. And it may not. Have fun debugging that! This is only working because it's easy to implement a 2D array as a 1D array, but to my knowledge this behaviour is not guaranteed. It is certainly not guaranteed with a vector or other conventional dynamic array solution. Please slap me down if I'm wrong.

#include <iostream>

using namespace std;

//begin function @ Seraph: Agreed. Lol.
int main()
{
    // ordering the array backwards to make the problem stand out better.
    // also made the array smaller for an easier demo
    int myStructure[4][4] = {{16,15,14,13},{12,11,10,9},{8,7,6,5}, {4,3,2,1}};
    int i = 0;

    // here we take the contents of the first element of the array
    for (int s = myStructure[0][0]; i < 16; i++, s++)
    {  //watch what happens as we increment it.
        cout << s << " ";
    }
    cout << endl;
    // output: 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 
    // this didn't iterate through anything. It incremented a copy of the first value

    // reset and try again
    i = 0;
    // this time we take an extra level of indirection 
    for (int * s = &myStructure[0][0]; i < 16; i++, s++)
    {
        // and output the value pointed at
        cout << *s << " ";
    }
    cout << endl;
    // output: 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
    // now we have the desired behaviour.
} //end function end Lol

output:

16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 
16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 
Sign up to request clarification or add additional context in comments.

5 Comments

The original declaration is MyStructure * myStructure[][], not int myStructure[][]. And those "begin/end function" comments are hilarious.
@Saraph Trying to make a point. OP needs an extra level of indirection to make the walk he's trying for. And even then he has to count on the rows/columns being contiguous in memory, something I do not believe is guaranteed.
@Saraph Holy smurph. Didn't know those begin/ends were still there! I cut and pasted the main from another ball of code I was playing with.
Well, if curly brackets somehow don't manage to hold the awesomeness, pascal will save the day!
1

If one loop is all you want, you could do it this way:

MyStructure* myStructure[8][8];

for(int i = 0; i<64; i++)
{
    MyStructure* s = myStructure[i/8][i%8];
}

You will iterate through every element of the matrix. However, time complexity is still O(rows*columns).

3 Comments

@EdS. yes, that's why i see no point in not using nested loops :)
And a hell of a lot safer.
With 8 the compiler has a lot of lee-way for masking and bitshifting, but I wonder what the performance is like in the general case with the / and % in there vs an extra add and branch on condition for the nested loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.