0

I've been revising on my coding skills recently and then I made a program that outputs the contents of a multidimensional array. It is simple but when I experimented with the code this is what that happened:

int dv[3][3] {
    {1,2,3},
    {4,5,6},
    {7,8,9}
};
for (auto col = dv; col != dv + 3; ++col) {
    for (auto row = *dv; row != *col + 3; ++row) {
        cout << *row << " ";
    }
}

Output:

1 2 3 1 2 3 4 5 6 1 2 3 4 5 6 7 8 9

Can anybody please tell me why is this happening?

5
  • "Can anybody please tell me what is happening?" What did you expect to happen? Commented Feb 28, 2015 at 15:31
  • Try changing auto row = *dv to auto row = *col Commented Feb 28, 2015 at 15:33
  • I want to know why is the output being shown like that. Commented Feb 28, 2015 at 15:34
  • I have changed auto row = *dv to auto row = *col in the code which I'm using in my system. But I want to know why is the output being shown like that when, auto = *dv. Commented Feb 28, 2015 at 15:38
  • Look at my answer below. I told you why. Commented Feb 28, 2015 at 15:42

2 Answers 2

2

Why does my code outputs so ?

Your error is inside the second loop initialization : auto row = *dv;. By doing so, you systematically come back to the beginning. Then, you go to *col + 3. Look at it this way :

First loop turn :

col = dv;
row = *dv;

Prints each number until row == *col + 3

Output : 1 2 3

Second loop turn :

col = dv + 3;
row = *dv;

Prints each number until row == *col + 3 but col is dv + 3

Output : 1 2 3 4 5 6 --> It started from the beginning (dv)

Total output with turn 1 and 2 : 1 2 3 1 2 3 4 5 6

Try this instead :

for (auto col = dv; col != dv + 3; ++col) {
    for (auto row = *col; row != *col + 3; ++row) { // (1)
        cout << *row << " ";
    }
}

// (1) : Starting at current `column` then printing until `column + 3`

Live example : https://ideone.com/Y0MKrW

Sign up to request clarification or add additional context in comments.

4 Comments

What is the difference between: dv and *dv ?
dv is your dv variables. Its value is the memory address of your array int[3][3]. *dv is the address of the first array inside dv. Memory is contiguous so the address of the first array inside dv is the address of dv. To summarize : dv == *dv.
So In the first loop turn as shown above where col = dv and row =*dv, here col is equal to the first array in dv and row is equal to the first element of the first array of dv. In the condition we check whether the dereferenced col that is 1 and when we add 3 to that 1 it becomes 4 and when we check whether they are unequal or not, it turns out that they are unequal and hence condition is true and we print up to 4. So my guess is that in this program we dereference col and add 3 to it and then that value is compared with row and when they are unequal row is printed. Is that right?
"dereferenced dv" is not 1. It id *dv which is the address of the first array in dv. (*dv is strictly equal to dv[0]). Almost everything is about memory addesses, here.
0

Your inner loop is starting at *dv. That is probably not what you meant to do.

Comments

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.