943,925 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 779
  • C++ RSS
Jun 11th, 2009
0

Return pointer

Expand Post »
IF i have this function...

C++ Syntax (Toggle Plain Text)
  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) ?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
JackDurden is offline Offline
92 posts
since Jun 2008
Jun 11th, 2009
0

Re: Return pointer

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]
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Jun 11th, 2009
0

Re: Return pointer

Yeah the node type def is this

C++ Syntax (Toggle Plain Text)
  1. struct node
  2. {
  3. vector <node*> childrent;
  4. int item;
  5. };

I dont want the data inside i want the pointer...
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
JackDurden is offline Offline
92 posts
since Jun 2008
Jun 11th, 2009
0

Re: Return pointer

>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.
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Jun 11th, 2009
0

Re: Return pointer

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) ?
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
JackDurden is offline Offline
92 posts
since Jun 2008
Jun 11th, 2009
1

Re: Return pointer

>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 )...
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Jun 11th, 2009
1

Re: Return pointer

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:

C++ Syntax (Toggle Plain Text)
  1. children[0] is null
  2. children[1] is null
  3. children[2] is null

Function should return null?

C++ Syntax (Toggle Plain Text)
  1. children[0] is null
  2. children[1] is not null
  3. children[2] is not null

Function should return children[1]?

C++ Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: help please))
Next Thread in C++ Forum Timeline: Hiding Implimentation from others, How!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC