Zbigniew,
I tried your suggestion. Unfortunately, it didnt quite do what I
wanted. What I am trying to do is to write a general purpose stack which
will have push and pop routines capable of processing any datatype. The
prototypes for these routines should be like :
void PushStack(<any datatype>);
<any datatype> PopStack(void);
Implementing the PushStack is not a problem. The PopStack routine
however, refuses to become tractable.
For starters, I had to give up on the function returning a value,
since I couldnt figure out how to make a macro return a value. Yes, the
example that you gave (below) does return a value, but I think that this
will only work with simple datatypes, and not with complex ones like
structures. However, even a prototype of the form 'void PopStack(<any
datatype>)' will also fail for the same reason. I could of course, insist
that the argument to the PopStack function be a pointer to the variable,
and this might work. But then, I'd lose the generality because if a
variable is declared as 'int i', I couldnt call PopStack as 'PopStack(&i)'
unless the macro is defined as '#define PopStack(&ITEM).....' and this
would preclude me from calling PopStack with a variable b of type 'struct
<some name> * b'.
Given the fact that the early C++ compilers were actually
preprocessors that decomposed C++ to C, I feel that there probably is a
way of implementing the template functionality in C. The only question is
how. Any ideas?
Kenneth
On Tue, 16 Mar 1999, Zbigniew Zagorski wrote:
> Hi !
>
> In C there aren't templates. But you can implement them in
> other way, you can use preprocessor #defines.
>
> For example you can see prototype of min, max functions in
> C
>
> #define min(a,b) ( (a)<(b) ? (a) | (b) )
> #define max(a,b) ( (a)>(b) ? (a) | (b) )
>
> As you see this functions will work with all numerical types
> as float,int,char or short.
>
> ----------===========----------
> Zbigniew Zagorski
> LO 12 Szczecin
> [EMAIL PROTECTED]
> ----------===========---------
>
> On Mon, 15 Mar 1999, Jor-el wrote:
>
> > Hi,
> >
> > Is there any way that I can implement the equivalent of the
> > template mechanism of C++ in C? That is, I need to pass a datatype as an
> > argument to a function.
> >
> > Kenneth
>
>