Evaluate the following statement?

How does the following statement evaluate?

  • I am having problem understanding as to what the following statement means(not the tertiary operator part) - return(((*data)*(tmp_left)<(*data)*(tmp_right))?tmp_left:tmp_right); where data is a pointer and tmp_left is a variable of type int. This line is in relation to a tree structure, if that helps. To be exact I want to know what does (*data)*(tmp_left) mean? Is it a value pointed to by the pointer or a member variable accessed by it? What does the asterisk '*' between the pointer and a variable signify?

  • Answer:

    Without knowing the data types, and knowing that it's used in a tree (assuming it's a BST) this looks like a comparison of data values. In essence, when you have a BST you will either insert nodes to the left of the immediate node or the right of the immediate node. It looks like the data value is contained within the node and is the pointer. So, (*data) derferences the node. The (*data) * tmp_left multiplies the values. The rest is a comparison statement. If the left hand side of the expression is less than the right hand size, use the tmp_left data value. Otherwise, continue to the right.

user597272 at Stack Overflow Visit the source

Was this solution helpful to you?

Other answers

(*data)*(tmp_left) - as data is a pointer, (*data) dereferences it (gets the value, pointed by data, then the value is multiplied by tmp_left (which, in your case, as you said, is int) The other parts should be clear :) EDIT: you can easily read such things by separating then into different parts return(((*data)*(tmp_left)<(*data)*(tmp_right))?tmp_left:tmp_right); 1. (*data) - get the value, pointed by data 2. tmp_left is an int 3. multiply (*data) and tmp_left (lets say the value is X) 4. (*data) - get the value, pointed by data 5. tmp_right is an int 6. multiply (*data) and tmp_right (lets say the value is Y) 7. now compare X and Y 8. if X &lt Y, return tmp_left, else return tmp_right

Kiril Kirov

(*data) * (tmp_left) is dereferencing the value of the data pointer, and multiplying the resulting value by tmp_left. Overall, if 'd' is the value pointed to by data, this says "if (d * tmp_left) is less than (d * tmp_right), return tmp_left, else return tmp_right".

dcw

What does the asterisk '*' between the pointer and a variable signify? http://en.wikipedia.org/wiki/Multiplication (in this context)

Prasoon Saurav

return(((*data)*(tmp_left)<(*data)*(tmp_right))?tmp_left:tmp_right); Lets simplify this, as, int a = (*data); //get the value from the address pointed to be 'data' int x = a * (tmp_left); //multiplication int y = a * (tmp_right); //multiplication //substitute x and y in the original expression, it becomes return ( x < y ? tmp_left : tmp_right ); Now it looks simple and easy to understand, right?

Nawaz

The * before a variable name as in *data is to dereference a pointer. The * between them is for multiplication. This code returns the smallest of tmp_right and tmp_left. It also appears to be functionally identical to: return((tmp_left)<(tmp_right)?tmp_left:tmp_right); I'm not sure what *data refers to, but unless something weird is going on the multiplication doesn't actually have any effect, as x*y < x*z is equivalent to y < z.

Ezra

When you see a complicated statement like that, trying simplifying it. First, break out the ternary operator and add some whitespace: if(((*data) * (tmp_left) < (*data) * (tmp_right))) { return tmp_left; } else { return tmp_right; } Next factor out *data and remove the redundant parens: data_type& d = *data; if((d * tmp_left < d * tmp_right)) { return tmp_left; } else { return tmp_right; } At this point it should be obvious that the * is multiplication, and exactly what's happening in the function. If *data is always positive then you could factor that term out completely but that's probably not the case.

Mark B

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.