1. Expected value, got address for *(matrix + 1)

Hi,

I was reading the C++ reference and ran across *(*(matrix + i) +j)
as a way to return data from a two index array (ie. matrix [i][j])
I'm trying to break it down to understand this notation better.
So I found *(matrix +1) is the same as (matrix +1). Basically the
address of the second set or row.
I would expect *(matrix +1) would be invalid or undefined since it to me
is dereferencing the entire second set or row of the multidimensional array.

Here is the program below:
Code:
```#include <iostream>

using namespace std;
int main(int argc, const char * argv[]) {
float matrix [2][2] = { {0.8,0.9} , {4.1,3.9}};
cout << matrix << " address of matrix" << endl;
cout << (matrix + 1) << " matrix + 1" << endl;
cout << *(matrix + 1) << " *(matrix + 1)"  << endl;
return 0;
}```

The output is the same address for *(matrix + 1) and (matrix + 1)

Thanks

2. The values are the same, but the types are different.
float (*m1)[2] = matrix + 1; // points to the entire 2nd row
float *m2 = *(matrix + 1); // points to the first element of the 2nd row

Like when you have a pointer to a structure, and a pointer to the first member - different types, same value.

3. Originally Posted by Salem
The values are the same, but the types are different.
float (*m1)[2] = matrix + 1; // points to the entire 2nd row
float *m2 = *(matrix + 1); // points to the first element of the 2nd row

Like when you have a pointer to a structure, and a pointer to the first member - different types, same value.

Thanks.
I guess my confusion is if (matrix + 1) is an address,
why is *(matrix + 1) not extracting or 'dereferencing' that value?

4. Originally Posted by FloridaJo
I guess my confusion is if (matrix + 1) is an address,
why is *(matrix + 1) not extracting or 'dereferencing' that value?
matrix + 1 is an address, but it points to another address. I.e., it's value is an address. To get the float value, you need to dereference it twice.

It's just silly to use endl instead of \n in this situation:
Code:
```    // Silly
cout << matrix << " address of matrix" << endl;
// Normal
cout << matrix << " address of matrix\n";```
Also, argv is not const and it's wrong to declare it that way.

5. Originally Posted by algorism
matrix + 1 is an address, but it points to another address. I.e., it's value is an address. To get the float value, you need to dereference it twice.

It's just silly to use endl instead of \n in this situation:
Code:
```    // Silly
cout << matrix << " address of matrix" << endl;
// Normal
cout << matrix << " address of matrix\n";```
Also, argv is not const and it's wrong to declare it that way.

Thanks, the address pointing to an address clears it up for me.
As for the other points, Steve Prata of C++ Primer uses the endl; and
that is the manual I am learning from at the moment.
Also, the first part containing "const * char argv" is the default line
when starting up Xcode. So you'll have to tell Apple Inc. their code is wrong.

6. Originally Posted by FloridaJo
As for the other points, Steve Prata of C++ Primer uses the endl;

endl is \n + flush

so if you just need end of line use \n
If you want to ensure the stream is flushed immediately after it - use endl