Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   I want o use the function alternate to a MACRO (http://www.velocityreviews.com/forums/t561177-i-want-o-use-the-function-alternate-to-a-macro.html)

parag_paul@hotmail.com 12-14-2007 09:36 PM

I want o use the function alternate to a MACRO
 

> I have a
> #define A(a,b,c,d)
> sdafasdf/
> sadfsadasd/
> sadfsad/
> sdf.


> Now, I have another #define A_function(a,b,c,d) {
> asdas;
> asdas;
> asd;}


> Now at runtime, I want to use the function version of the MACRO .
> I dont want to use the MACRO, since it is not debuggable
> Please help


pete 12-14-2007 09:54 PM

Re: I want o use the function alternate to a MACRO
 
parag_paul@hotmail.com wrote:

> > Now at runtime, I want to use the function version of the MACRO .
> > I dont want to use the MACRO, since it is not debuggable
> > Please help


/* BEGIN new.c */

#include <stdio.h>

#define add_5(X) ((X) + 5)

int (add_5)(int x)
{
return add_5(x);
}

int main(void)
{
int x = 6;

/*
** This uses the macro directly.
*/
printf("%d plus 5 equals %d\n", x, add_5(x++));



/*
** This uses the function.
*/
printf("%d plus 5 equals %d\n", x, (add_5)(x));
return 0;
}

/* END new.c */


--
pete

Peter Nilsson 12-15-2007 12:38 AM

Re: I want o use the function alternate to a MACRO
 
"parag_p...@hotmail.com" <parag_p...@hotmail.com> wrote:
> > I have a
> > #define A(a,b,c,d)


I presume you meant to add a \ at the end.

> > sdafasdf/
> > sadfsadasd/
> > sadfsad/
> > sdf.


See...

http://c-faq.com/cpp/multistmt.html

> > Now, I have another #define A_function(a,b,c,d) {


Since this is another macro, but has a different name
to the previous macro, I don't quite see what your real
question is (though pete has answered what I think you
want.)

> > asdas;
> > asdas;
> > asd;}
> > Now at runtime, I want to use the function version of
> > the MACRO .


Macro's stop existing even before compilation. So it's
meaningless to say you do or don't want a macro to run
at runtime.

Please be careful with your terminology, both the macros
you've supplied are function macros. So it's not obvious
what you mean by "function version the MACRO."

> > I dont want to use the MACRO, since it is not debuggable


Perhaps you should look into inline functions, though they
generally suffer similar problems. This is where log files
are often superior to debuggers.

--
Peter


All times are GMT. The time now is 07:39 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.