| | |
Pointers to struct
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
If I have this struct :
and I have this function
How can I pass the tree right pointer again into the blah function, I tried using blah ( tree -> rightp ), but it doesn't work, any help on how to do this ? :o
Thanks in advance
C++ Syntax (Toggle Plain Text)
typedef struct node { int key; struct node *leftp; struct node *rightp; } *Treep;
and I have this function
C++ Syntax (Toggle Plain Text)
int blah (Treep *tree);
How can I pass the tree right pointer again into the blah function, I tried using blah ( tree -> rightp ), but it doesn't work, any help on how to do this ? :o
Thanks in advance
C++ Syntax (Toggle Plain Text)
int blah (Treep *tree);
I think the data type you are attempting to pass is incorrect.. you should be passing in an object of type, "node"
C++ Syntax (Toggle Plain Text)
int blah (node *tree);
please post compiler errors for more assistance.
Thanks, but the arguments of the function is right, however what I am having trouble with is this, the function is recursive, and when I use blah( tree -> rightp ) the compiler raise this error :
How I am I supposed to pass the argument of blah ?
C++ Syntax (Toggle Plain Text)
blah.c:35: error: request for member `rightp' in something not a structure or union
How I am I supposed to pass the argument of blah ?
>I tried using blah ( tree -> rightp )
Why? tree->rightp is a node*, but the function expects a node** because you typedef'd Treep to be a node*, and blah asks for a Treep*. The progression is like this:
Treep becomes struct node*
so
Treep* becomes struct node**
So you should be calling blah like this:
Your confusion on this point suggests that you shouldn't be hiding a level of indirection behind a typedef. Declare your struct as:
And then you'll know that Tree* is the same as node*, and Tree** is the same as node**. Hiding levels of indirection is a bad practice anyway because it confuses so many people.
Why? tree->rightp is a node*, but the function expects a node** because you typedef'd Treep to be a node*, and blah asks for a Treep*. The progression is like this:
Treep becomes struct node*
so
Treep* becomes struct node**
So you should be calling blah like this:
C++ Syntax (Toggle Plain Text)
blah ( &tree->rightp )
C++ Syntax (Toggle Plain Text)
typedef struct node { int key; struct node *leftp; struct node *rightp; } Tree;
New members chased away this month: 4
•
•
•
•
Originally Posted by Narue
I invite your instructor to come here and get schooled about why his code is a bad idea.
(¯`·._.· Swaying with the wind ·._.·´¯)
>I think this will work fine.
You thought wrong. As I so carefully described, node* and Treep* are different types because Treep hides a level of indirection. Your suggestion fails for three very good reasons:
1) The function is an instructor provided function that cannot be modified.
2) You would change the functionality of the code (see below).
3) The function would be a no-op after your change (see below).
The functionality of the code would be changed because the blah function expects a node**, but you end up passing it a node*. This would cause the compiler to complain, and in your infinite wisdom, you would probably change the contents of blah to work with a node*, thus leading you to the next problem.
When working with self-referential data structures like a binary search tree, you need some way to return the modified structure of the tree back to the calling function. My preferred method is to return a pointer to the new tree:
But an alternative method is to pass a pointer to the pointer so that changes are saved because you have access to the original pointer and not just a copy of it. This way you can return a status code:
Does that look familiar? If not, try this, it's equivalent:
The extra level of indirection is thus required for changes made to the tree (assuming that blah modifies the tree) to be saved outside of the function. Your suggestion removes that level of indirection, which changes the logic of the code and breaks it.
Don't think. Test. If you don't know, don't post until you do.
You thought wrong. As I so carefully described, node* and Treep* are different types because Treep hides a level of indirection. Your suggestion fails for three very good reasons:
1) The function is an instructor provided function that cannot be modified.
2) You would change the functionality of the code (see below).
3) The function would be a no-op after your change (see below).
The functionality of the code would be changed because the blah function expects a node**, but you end up passing it a node*. This would cause the compiler to complain, and in your infinite wisdom, you would probably change the contents of blah to work with a node*, thus leading you to the next problem.
When working with self-referential data structures like a binary search tree, you need some way to return the modified structure of the tree back to the calling function. My preferred method is to return a pointer to the new tree:
C++ Syntax (Toggle Plain Text)
struct node *blah ( struct node *tree );
C++ Syntax (Toggle Plain Text)
int blah ( struct node **tree );
C++ Syntax (Toggle Plain Text)
typedef struct node *Treep; int blah ( Treep *tree );
Don't think. Test. If you don't know, don't post until you do.
New members chased away this month: 4
I am trying to allocate memory for tree pointer, in the blah function, by doing this :
However, the compiler gives this error
but it works fine if I use
Everything works fine, why is this ? how am I supposed to allocate memory for the first case ?
Thanks
C++ Syntax (Toggle Plain Text)
*tree = ( Treep * ) malloc ( sizeof ( Treep ) ) ;
However, the compiler gives this error
C++ Syntax (Toggle Plain Text)
blah.c:88: warning: assignment from incompatible pointer type
but it works fine if I use
C++ Syntax (Toggle Plain Text)
typedef struct node { int key; struct node *leftp; struct node *rightp; } *Treep; int blah (Treep **tree);
Everything works fine, why is this ? how am I supposed to allocate memory for the first case ?
Thanks
(¯`·._.· Swaying with the wind ·._.·´¯)
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: Help with program
- Next Thread: Accessing VB DLL from VC++
Views: 4380 | Replies: 11
| Thread Tools | Search this Thread |
Tag cloud for C++
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 email 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






