Return pointer

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jun 2008
Posts: 92
Reputation: JackDurden is an unknown quantity at this point 
Solved Threads: 0
JackDurden JackDurden is offline Offline
Junior Poster in Training

Return pointer

 
0
  #1
Jun 11th, 2009
IF i have this function...

  1. node* get_lowest(node* root)
  2. {
  3. node* min = root->child[0];
  4.  
  5. for(int i = 0; i<root->child.size();i++)
  6. {
  7. if(min > root->child[i])
  8. min = root->child[i];
  9. }
  10. cout<<"Here it is:"<<min->data<<endl;
  11. return min;
  12. }

how do I get it to return the smallest indexed pointer?
for example if root->child[0] and root->child[1] exist how do I get root->child[0] to return?

return_object = get_lowest(root) ?
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Return pointer

 
0
  #2
Jun 11th, 2009
Probably you want a pointer which points to the minimal data value. In actual fact it's (as usually) senseless operation to compare pointer values. Moreover, the result of pointers comparison is well-defined only if two pointers points to the same array.

If so, compare data values, not pointers. Save pointer value which refers to the minimal data value. Return this pointer - that's all.

Can you post node type definition? It's hard to say more without this info...

Next time use code tag with the language specifier:
[code=cplusplus]
source text
[/code]
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 92
Reputation: JackDurden is an unknown quantity at this point 
Solved Threads: 0
JackDurden JackDurden is offline Offline
Junior Poster in Training

Re: Return pointer

 
0
  #3
Jun 11th, 2009
Yeah the node type def is this

  1. struct node
  2. {
  3. vector <node*> childrent;
  4. int item;
  5. };

I dont want the data inside i want the pointer...
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Return pointer

 
0
  #4
Jun 11th, 2009
>I dont want the data inside i want the pointer
Which pointer do you want (and why) - that's a question

If you want the minimal pointer value ( why?! ) - you have it. What's a problem? Well, there are two defects in your function:
1. if child (or childrent) size == 0 then root->child[0] raises memory access exception.
2. Start the loop from i = 1.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 92
Reputation: JackDurden is an unknown quantity at this point 
Solved Threads: 0
JackDurden JackDurden is offline Offline
Junior Poster in Training

Re: Return pointer

 
0
  #5
Jun 11th, 2009
I want the minimal pointer because it is the left most node for my tree. If i have it how do I get it in a variable like this-

root->children[0] = get_lowest(root) ?
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Return pointer

 
1
  #6
Jun 11th, 2009
>I want the minimal pointer because it is the left most node for my tree.
The leftmost node in the tree is not the same as the minimal pointer!
Moreover, a pointer value does not bear a relation to the node position in the tree.
Make a loop from the root via child[0] pointers until you have found a leaf - that's a node with an empty child vector. That's the leftmost node of the tree (probably )...
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,836
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Return pointer

 
1
  #7
Jun 11th, 2009
Just a hunch, but are you trying to find, among all the non-null pointers in children , the one with the lowest index?

For example assume that the children vector has three elements:

  1. children[0] is null
  2. children[1] is null
  3. children[2] is null

Function should return null?

  1. children[0] is null
  2. children[1] is not null
  3. children[2] is not null

Function should return children[1]?

  1. children[0] is not null
  2. children[1] is not null
  3. children[2] is null

Function should return children[0]?

Is that the idea? I agree with ArkM. Comparing two pointers and deciding which is "less than" the other (presumably by comparing the addresses?) doesn't make a lot of sense.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC