TIP: Click on subject to list as thread! ANSI
echo: cis.languages
to: Jay Truesdale 72176,3565 (X)
from: Pete Lyall 76703,4230
date: 1990-12-11 00:29:36
subject: #8674-Dynamic Structure Alloc

#: 8675 S3/Languages
    11-Dec-90  00:29:36
Sb: #8674-Dynamic Structure Alloc
Fm: Pete Lyall 76703,4230
To: Jay Truesdale 72176,3565 (X)

Jay -

First of all, great questions! Now lets see if I can answer them.... The
statement where you do root_ptr = (struct b_tree_rec *) malloc(sizeof(node));
. root_ptr is declared to be of type 'struct b_tree_rec *' above... malloc is
of type 'char *'. Since a pointer to a char is a different animal than a
pointer to a 'struct b_tree_rec' (NOTE: pointer math and size of objects was
right on ethe money), then we must 'cast' or 'coerce' the source type to
produce the same type as expected by the receiving variable, in this case
rec_ptr. Some compilers will blow you out if you try to assign different types,
and some are very lax.

On the getting to the second node, I believe it'd be something like this:

 node_ptr = (struct b_tree_rec *) malloc(sizeof(node));
  ... error checking ...

 ... allocation and linking of subsequent notes ....

  /* get at left node and determine the key */

  curr_node = root_ptr->lp;
  curr_key = curr_node->key;

Hope that helps....

Pete

SOURCE: compuserve via textfiles.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.