Pointers to struct

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2005
Posts: 13
Reputation: l-o-s-t is an unknown quantity at this point 
Solved Threads: 0
l-o-s-t's Avatar
l-o-s-t l-o-s-t is offline Offline
Newbie Poster

Pointers to struct

 
0
  #1
Jan 21st, 2006
If I have this struct :

  1. typedef struct node {
  2. int key;
  3. struct node *leftp;
  4. struct node *rightp;
  5. } *Treep;

and I have this function

  1. 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
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 481
Reputation: Clinton Portis is on a distinguished road 
Solved Threads: 58
Clinton Portis's Avatar
Clinton Portis Clinton Portis is offline Offline
Posting Pro in Training

Re: Pointers to struct

 
0
  #2
Jan 21st, 2006
  1. 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"

  1. int blah (node *tree);


please post compiler errors for more assistance.
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 13
Reputation: l-o-s-t is an unknown quantity at this point 
Solved Threads: 0
l-o-s-t's Avatar
l-o-s-t l-o-s-t is offline Offline
Newbie Poster

Re: Pointers to struct

 
0
  #3
Jan 21st, 2006
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 :
  1. blah.c:35: error: request for member `rightp' in something not a structure or union
  2.  

How I am I supposed to pass the argument of blah ?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,848
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 754
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: Pointers to struct

 
0
  #4
Jan 21st, 2006
>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:
  1. blah ( &tree->rightp )
Your confusion on this point suggests that you shouldn't be hiding a level of indirection behind a typedef. Declare your struct as:
  1. typedef struct node {
  2. int key;
  3. struct node *leftp;
  4. struct node *rightp;
  5. } Tree;
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.
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 13
Reputation: l-o-s-t is an unknown quantity at this point 
Solved Threads: 0
l-o-s-t's Avatar
l-o-s-t l-o-s-t is offline Offline
Newbie Poster

Re: Pointers to struct

 
0
  #5
Jan 21st, 2006
Thanks Narue

you shouldn't be hiding a level of indirection behind a typedef
I would have declared it in the way u suggested, however I was meant to be using the struct and the prototype of blah as defined in the lab specification.

Thanks again
(¯`·._.· Swaying with the wind ·._.·´¯)

Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,848
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 754
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: Pointers to struct

 
0
  #6
Jan 21st, 2006
>I was meant to be using the struct and the prototype of blah as defined in the lab specification.
As usual, I invite your instructor to come here and get schooled about why his code is a bad idea.
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 13
Reputation: l-o-s-t is an unknown quantity at this point 
Solved Threads: 0
l-o-s-t's Avatar
l-o-s-t l-o-s-t is offline Offline
Newbie Poster

Re: Pointers to struct

 
0
  #7
Jan 21st, 2006
Originally Posted by Narue
I invite your instructor to come here and get schooled about why his code is a bad idea.
I wish, this guy has a funny way of teaching things, and like to make things as complicated as possible, he is good at giving hideous assignments and lab work, provided with unclear specifications, and no test data *sigh*
(¯`·._.· Swaying with the wind ·._.·´¯)

Reply With Quote Quick reply to this message  
Join Date: Jan 2006
Posts: 40
Reputation: beuls is an unknown quantity at this point 
Solved Threads: 2
beuls beuls is offline Offline
Light Poster

Re: Pointers to struct

 
0
  #8
Jan 22nd, 2006
I think this will work fine.

[code]
typedef struct node {
int key;
struct node *leftp;
struct node *rightp;
} *Treep;


void blah(node *tree)
{

}
[code]
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,848
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 754
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: Pointers to struct

 
0
  #9
Jan 22nd, 2006
>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:
  1. struct node *blah ( struct node *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:
  1. int blah ( struct node **tree );
Does that look familiar? If not, try this, it's equivalent:
  1. typedef struct node *Treep;
  2. int blah ( Treep *tree );
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.
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 13
Reputation: l-o-s-t is an unknown quantity at this point 
Solved Threads: 0
l-o-s-t's Avatar
l-o-s-t l-o-s-t is offline Offline
Newbie Poster

Re: Pointers to struct

 
0
  #10
Jan 24th, 2006
I am trying to allocate memory for tree pointer, in the blah function, by doing this :
  1. *tree = ( Treep * ) malloc ( sizeof ( Treep ) ) ;

However, the compiler gives this error
  1. blah.c:88: warning: assignment from incompatible pointer type

but it works fine if I use
  1. typedef struct node {
  2. int key;
  3. struct node *leftp;
  4. struct node *rightp;
  5. } *Treep;
  6.  
  7. 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 ·._.·´¯)

Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum


Views: 4380 | Replies: 11
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC