Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

precedence of increment and de-reference

Status
Not open for further replies.

bkelly13

Programmer
Aug 31, 2006
98
US
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)
 
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?
 
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)
 
>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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top