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

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    161

    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. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    161
    Quote Originally Posted by Salem View Post
    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. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by FloridaJo View Post
    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. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    161
    Quote Originally Posted by algorism View Post
    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. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by FloridaJo View Post
    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
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 01-06-2016, 01:15 AM
  2. Replies: 2
    Last Post: 05-19-2014, 07:32 PM
  3. Replies: 1
    Last Post: 02-25-2013, 08:08 AM
  4. Replies: 1
    Last Post: 11-07-2010, 11:39 PM
  5. Block address from word address
    By xddxogm3 in forum Tech Board
    Replies: 0
    Last Post: 04-25-2007, 09:02 PM

Tags for this Thread