|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Macro
Is there a way to output results in form of values from a macro ?
|
|
#3
|
||||
|
||||
|
#define OutputResults( res ) printf( res )
Afraid you may have to be a bit more specific. What are you trying to "output" and where are you trying to "output" it? Why on earth would you want to use a macro for this purpose?
__________________
My worst nightmare was a pointless infinite loop. Work in progress; don't poke the curmudgeon! http://www.odonahue.com/ |
|
#4
|
|||
|
|||
|
#define OutputResults( res ) printf( res )
Someone calls like: OutputResults("%x%x%x"); ![]() |
|
#5
|
||||
|
||||
|
Quote:
![]() |
|
#6
|
|||
|
|||
|
Macro
We know that a Macro can be of multiple lines. Suppose i want a function a to be a marco which just returns a value of pointer.
Something like int value(int x) { int *ptr; ptr=x; return *ptr; } This function will return the value . If we want this function as a macro then we can write #define value(x) int *ptr ; / ptr=x; / return *ptr; / // This may not work I want to replace return *ptr to return something with a macro. I hope i am clear now. ![]() |
|
#7
|
||||
|
||||
|
You are obviously confused. Perhaps you could give an example of how you want to actually use value()?
|
|
#8
|
||||
|
||||
|
You're more than confused. You're rude. Whether that's arrogance or ignorance, I don't know. I do know that you should read the "New users - HOW TO POST A QUESTION - READ THIS FIRST" thread, as well as the other FAQs.
One presumes that a wannabe programmer can read, can spot sticky threads, and can comprehend. Possibly that's an erroneous assumption.
__________________
Write no code whose complexity leaves you wondering what the hell you did. Politically Incorrect DaWei on Pointers Grumpy on Exceptions |
|
#9
|
|||||
|
|||||
|
Quote:
So why did your original post not simply say "is it possible to return a value from a macro?" ? What you did post was unintelligible, even now I know what you mean! Quote:
Code:
*((int*)x) ; Code:
int z = [code]int z = *((int*)(x)) ; Quote:
All this nonsense is one of the many reasons why macros are generally considered a 'bad-thing'(tm). My advice is not to do this at all. However for a macro to 'appear' to behave like a function and return a value - it must simply evaluate to a value. That generally precludes code that cannot be written as a single arithmetic/logic expression, although some voodoo with the comma operator and side effects may allow you to perform some tasks, while only getting you into even deeper macro-hell! In the case of your example, it is in fact simple, because as I already pointed out you rather over-complicated the function in the first instance. Code:
#define value(x) *((int*)(x)) Code:
int z = value(x) ; the preprocessor will simply generate: Code:
int z = *((int*)(x)) ; Now you will notice in this macro an extra pair of parentheses around the argument x in the definition. This is to avoid another problem of macro-hell; x need not be a simple value of variable, but may itself be an expression. Without the parentheses, when placed in the context of the macro definition, the evaluation order may result in unintended results. Hopefully what you have learned is not how to do it, but why doing it is a bad idea in the first instance. And there are more issues with macros beside those I have pointed out. In this case the code in your original function looks like nonsense to me, but at least as a function you will have an easier time debugging it. For example, any compiler messages will be issued for the replacement text not the macro; this may make the messages very confusing, and they will be issued where the macro is used, not where it is defined, and because the preprocessor performs dumb replacement, the code line may look perfectly legitimate, while the unseen replacement renders it nonsense. Moreover, when you do get it to compile and it still does not work, you cannot step through the preprocessor definition like you can a function. Use macros sparingly, and for the simplest possible cases. Do not use then because you thing it provides an optimisation; it is most often either not true or not significant. And never use then where an argument appears more than once in a definition. Where an expression is passed, that expression will be evaluated more than once - likely obviating any perceived benefit from avoiding the function call overhead. Clifford Last edited by clifford : November 3rd, 2009 at 04:22 PM. |
|
#10
|
|||
|
|||
|
Quote:
May be it would be of help !! . The only thing wanted to debate is if we can return from a macro.I think there is a trick to do so by using do{}while(0); I am sorry if i had confused you. |
|
#11
|
||||
|
||||
|
Please just give an example of how you want to use this macro. Then maybe we can help you.
Macros don't actually "return" anything as in the sense that functions have return types/values. Macros emit inline code. |
|
#12
|
|||
|
|||
|
Quote:
Sure i will make a point to read them before my next post. |
|
#13
|
|||
|
|||
|
Quote:
Thanks !! . I just need to use a macro instead of a function call. The problem i am locked in is " Is there a way to assign values from a multiline macro. I know single like is like a statement and we can easily assign the values by using one of those." I want to emulate return functionality of a function call by using a macro.I think there is a trick to do so. |
|
#14
|
||||
|
||||
|
You're not making sense at all. Give me a good reason why you want to use a macro when you have a multi-line statement. Why can you NOT use a function? Using a macro to do a function's work is bad practice anyway, especially since C++ and C99 both support the inline keyword now. A function works better than a macro since it doesn't have unexpected side-effects.
__________________
Up the Irons What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home. "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest Down with Sharon Osbourne Puzzle of the Month solved by Fishmonger, superior perl programmer of the month |
|
#15
|
||||
|
||||
|
An example usage please. Quit defining the problem. Show me how you are going to use this macro. For instance:
int x = value(5) ; How will the macro be used? If value() is a multi-line macro, you can't use it like the example above, you will get a syntax error. There are no tricks, just valid or invalid code. There may in fact be some valid hack, though unlikely, that will work with some specific example, but you're going to have to correctly define the context in which this macro will be evaluated before anyone can say for certain. It really is unlikely that you can achieve what-ever it is you think you want to do, using a macro, but we can't know for sure until you give an explicit example how you want to use it. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > Macro |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|