×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

precedence of increment and de-reference

precedence of increment and de-reference

precedence of increment and de-reference

(OP)
One of my learning books said that the dereference operator had the same precedence as incrrement and decrement operators.  Doubting that I searched and found that de-reference is the same as prefix increment and decrement, but less than postfix.  Just for grins I ran a test.  Now I think my test is flawed, but cannot see where.  Here is the code I wrote for Visual Studio C++ running under Windows 7:

CODE

    std::string a = "ABC";
    std::string d = "DEF";
    std::string h = "HIJ";
    std::string k = "KLM";

    std::string x;

    string::iterator a_itr = a.begin();
    string::iterator d_itr = d.begin();
    string::iterator h_itr = h.begin();
    string::iterator k_itr = k.begin();

    x.push_back(*a_itr );
    x.push_back(*d_itr );
    x.push_back(*h_itr );
    x.push_back(*k_itr );
    x.push_back( '_' );


    x.push_back(* ++ a_itr );    // incremented then de-referenced
    x.push_back( * d_itr ++ );   // de-referenced then incremented
    x.push_back( *( ++ h_itr) ); // incremented then de-referenced
    x.push_back( ++( * k_itr) ); // incremented then de-referenced
After running the code x contains  ADHK_BDIL
VS won't let me cut from the Locals page, but I hope I have the output right.
The problem is the last line of code and the last character in the string x.  To my opinion the parens should force the k_itr to be de-reference before the increment.  The results indicate that it is incremented then de-referenced.  
What is going on here?

We need to know what a dragon is
before we study its anatomy.
(Bryan Kelly, 2010)
 

RE: precedence of increment and de-reference

CODE

x.push_back( ++( * k_itr) ); // incremented then de-referenced
Let's forget a strange comment.

Evaluate:

1. k_itr is dereferenced (references to the 1st char of k - that's 'K' letter).
2. increment this letter in k (K => L), yields new (incremented) VALUE: 'L'
3. push back 'L' to x.

What's a problem?
 

RE: precedence of increment and de-reference

(OP)
When k_itr is created it points to the first letter of its string, K.  

The second reference to k_itr is used to put the letter K into the string x.  The iterator is not incremented.  This demonstrates that it starts its life pointing to K.

The third third time it is used the parenthisis says to dereference k_itr, THEN to increment.  The fact that L shows up in string x indicates the k_itr was incremented BEFORE it was dereferenced. That is in conflict with the parethisis.  Am I misinterperting the statement.

Yes, its a purely artifical contrivance and will never see the insides of a program in other than a test environment.  Just the same, it serves as a demonstration vehicle.

We need to know what a dragon is
before we study its anatomy.
(Bryan Kelly, 2010)
 

RE: precedence of increment and de-reference

>The fact that L shows up in string x indicates the k_itr was incremented BEFORE it was dereferenced.
Alas, it's a wrong statement. Reread my prev post again: k_itr WAS NOT INCREMENTED in your snippet.
 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close