Variable Parameters in C functions
Chuck Urwiler
chuck at umiami.ir.miami.edu
Fri Jan 11 07:23:04 AEST 1991
Hello.
I was wondering if any of you out there have messed around with functions
that use variable paramaters. I have implemented a binary tree structure
and my insert function requires that I use a variable paramater, to change
the actual global data, not the local. I have several trees that I'm going
to be using in this program, so just using the global variables won't work.
I've managed to create some code that works, but I've found that it's not
"clean" and will not compile on other machines besides the VAX. Here's the
code I've written:
#define NILP 0L
struct duration { int data;
struct duration *lchild,*rchild;
int quant;};
struct duration *dur,*top;
main()
{ dur=NILP;
menu();
}
menu()
{... insert(dur,&dur,newnum); ... }
insert(node,add,ndata)
int ndata,*add;
struct duration *node;
{ struct duration *temp;
if (node==NILP)
{ temp=malloc(sizeof(struct duration));
if (temp!=0)
{ temp->lchild=NILP;
temp->rchild=NILP;
temp->data=ndata;
*add = temp;
}
else
puts("Can't allocate more space.");
}
else if (ndata < node->data)
insert(node->lchild,&(node->lchild),ndata);
else
insert(node->rchild,&(node->rchild),ndata);
}
I assume that somewhere along the line that I'm breaking some rules, or
maybe some compilers are telling me that this code is gonna scribble all
over uncharted places in memory. Any suggestions you may have to "clean"
up this code, or other ideas you may have to do the same trick, please
e-mail me a reply. Any help would be greatly appreciated!
--
Chuck Urwiler University of Miami Music Engineering Voice & Keys
===========================================================================
chuck at umiami -or @umiamivm (Bitnet) |"Sometimes, the one thing that you're
chuck at umiami.ir.miami.edu (Internet) | looking for, it's the last thing on
chuck at miavax.ir.miami.edu (Internet) | your mind" -Robbie Nevil
pannet!curwiler at uunet.uu.net (PAN) |
===========================================================================
Disclaimer: I only work and learn at this University. I don't speak for it!
More information about the Comp.lang.c
mailing list