Thread: C++11 for statement and addresses:

  1. #1
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499

    C++11 for statement and addresses:

    Why is C++11 act different than C++ in reference to the for statements.

    I was pretty excited when I read how they worked in my textbook and now I am left with questions. Why am I getting different addresses?

    Code:
     #include <iostream>
    #include <array>
    
    using namespace std;
    
    void changePointer(int change[])
    {
            cout<<change<<endl;
    }
    
    int main(int argc, const char * argv[])
    {
    
        array <int, 5> drew={34,22,55,56,98};
        
        for(int items : drew)
            cout<<"The arrays is:"<<items<<endl;
            
        for (int change : drew) {
            cout<<"The address is:"<<&change<<endl;   //c++11 is suppose to do the same as below
        }
        
        for (int i=0; i<5; i++) {
            
        changePointer(&drew[i]);     // this did what I thought it would
            
        }
        
        return 0;
    }

  2. #2
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    I think it might be because using int[] tells your compiler or w/e to make a static copy of whatever you're passing to it. You're passing the value of an address and where that value is stored is what you're printing, right?

    Is that right? I'm probably wrong but I figured, eh, the internet, right?

    What if you printed changed[0]?

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by jocdrew21
    Why am I getting different addresses?
    In your first and second for loops, a copy of each element is made in order to initialise items and change respectively on each iteration of the loop, hence the address printed is the address of the variable to which the copy was made. In your third for loop, you access each element of the array without copying, hence the address printed is the address of the element.

    If you want change to refer to the current element of the array instead of being a copy, make it a reference variable, e.g.,
    Code:
    for (int& change : drew) {
        cout << "The address is:" << &change << endl;
    }
    Thus, change is an alias for the current element of drew, hence &change will result in the address of the current element of drew.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    While the third loop is printing the addresses of the individual elements of the int array, the second loop is printing the address of the integer variable chnage, which occupies a fixed position.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    Your amazing!!!!

    Hopefully one day I understand programing like you laser light !!!

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    looking at the for loop definition I see it would be extracted to something similar to

    Code:
    for(...)
    {
       int change = *&drew[i];
       cout<<"The address is:"<<&change<<endl; 
    }
    so you are printing the address of for-range-declaration variable. Why do you think it should match address of array member?

    For a range-based for statement of the form
    for ( for-range-declaration : expression ) statement
    let range-init be equivalent to the expression surrounded by parentheses89
    ( expression )
    and for a range-based for statement of the form
    for ( for-range-declaration : braced-init-list ) statement
    let range-init be equivalent to the braced-init-list. In each case, a range-based for statement is equivalent
    to
    Code:
    {
      auto && __range = range-init;
      for ( auto __begin = begin-expr,
        __end = end-expr;
        __begin != __end;
        ++__begin ) {
          for-range-declaration = *__begin;
          statement
        }
    }
    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

  7. #7
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    If I change the value at the location of the variable, I change it everywhere right. Thus it should print it out whenever I output it after I changed it. What am I missing? I thought I did this below.

    Code:
     #include <iostream>
    #include <array>
    
    using namespace std;
    
    int changer(int *a)
    {
        return *a * 4;
    
    }
    
    
    
    int main(int argc, const char * argv[])
    {
    
        array <int, 5> drew={34,22,55,56,98};
        
        for(int items : drew)
            cout<<"The arrays is:"<<items<<endl;
            
        for (int& change : drew) {
            cout<<"The address is:"<<&change<<endl;   
        }
        
        
        for(int &i : *&drew)            //pass a copy and did simple arithmetic
            
            cout<< i*6 <<endl;
        
        cout<<changer(&drew[0])<<endl;              // changed the value with a pointer
        
        for(int items : drew)
            cout<<"The arrays is now:"<<items<<endl;      // Here the value should be the same as above
        
        return 0;
    }

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    > *a * 4;

    This is not an assignment. You need to store the result, for example:

    *a *= 4;

  9. #9
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    ahhhh... Thanks... Finally I get pointers and pass by reference. It's lovely how programing can make you feel like a complete retard at times.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 08-19-2011, 06:53 PM
  2. problem in addresses
    By elwad in forum C Programming
    Replies: 4
    Last Post: 09-08-2009, 09:35 AM
  3. MAC addresses
    By Thantos in forum Networking/Device Communication
    Replies: 2
    Last Post: 01-20-2004, 02:26 PM
  4. DMA, IO Addresses and IRQ
    By jrahhali in forum Tech Board
    Replies: 1
    Last Post: 08-31-2003, 09:09 AM
  5. Memory Addresses
    By Breetai in forum C++ Programming
    Replies: 6
    Last Post: 12-10-2002, 08:10 AM