| | |
Return pointer
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Jun 2008
Posts: 92
Reputation:
Solved Threads: 0
IF i have this function...
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) ?
C++ Syntax (Toggle Plain Text)
node* get_lowest(node* root) { node* min = root->child[0]; for(int i = 0; i<root->child.size();i++) { if(min > root->child[i]) min = root->child[i]; } cout<<"Here it is:"<<min->data<<endl; return min; }
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) ?
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]
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]
•
•
Join Date: Jun 2008
Posts: 92
Reputation:
Solved Threads: 0
Yeah the node type def is this
I dont want the data inside i want the pointer...
C++ Syntax (Toggle Plain Text)
struct node { vector <node*> childrent; int item; };
I dont want the data inside i want the 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
2. Start the loop from i = 1.
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.
>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
)...
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
)... •
•
Join Date: Jan 2008
Posts: 3,844
Reputation:
Solved Threads: 503
Just a hunch, but are you trying to find, among all the non-null pointers in
For example assume that the
Function should return null?
Function should return children[1]?
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.
children , the one with the lowest index?For example assume that the
children vector has three elements: C++ Syntax (Toggle Plain Text)
children[0] is null children[1] is null children[2] is null
Function should return null?
C++ Syntax (Toggle Plain Text)
children[0] is null children[1] is not null children[2] is not null
Function should return children[1]?
C++ Syntax (Toggle Plain Text)
children[0] is not null children[1] is not null 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.
![]() |
Similar Threads
- How to return pointer to object & avoid memory leak? (C++)
- Return pointer to array of a struct (C++)
- Return Array from C++ (C++)
- total newb - "passing arg 2 of `strcpy' makes pointer from integer without a cast" (C++)
- pointer (C)
- Return multidimension array (C)
Other Threads in the C++ Forum
- Previous Thread: help please))
- Next Thread: Hiding Implimentation from others, How!
Views: 501 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez graph homeworkhelp iamthwee ifstream input int java lib lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort sorting spoonfeeding string strings struct temperature template templates text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






