C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 2nd, 2009, 04:37 PM
dspgro dspgro is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2009
Posts: 46 dspgro Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 6 h 33 m 2 sec
Reputation Power: 0
Macro

Is there a way to output results in form of values from a macro ?

Reply With Quote
  #2  
Old November 2nd, 2009, 04:38 PM
IOI-RLZ's Avatar
IOI-RLZ IOI-RLZ is offline
BANDITS 6'0 Clock !
Dev Shed Novice (500 - 999 posts)
 
Join Date: Aug 2006
Location: at the top of the stairway to heaven.
Posts: 695 IOI-RLZ User rank is First Lieutenant (10000 - 20000 Reputation Level)IOI-RLZ User rank is First Lieutenant (10000 - 20000 Reputation Level)IOI-RLZ User rank is First Lieutenant (10000 - 20000 Reputation Level)IOI-RLZ User rank is First Lieutenant (10000 - 20000 Reputation Level)IOI-RLZ User rank is First Lieutenant (10000 - 20000 Reputation Level)IOI-RLZ User rank is First Lieutenant (10000 - 20000 Reputation Level)IOI-RLZ User rank is First Lieutenant (10000 - 20000 Reputation Level)IOI-RLZ User rank is First Lieutenant (10000 - 20000 Reputation Level)  Folding Points: 7098 Folding Title: Novice Folder
Time spent in forums: 2 Weeks 1 Day 21 h 51 m 28 sec
Reputation Power: 195
Send a message via MSN to IOI-RLZ Send a message via Yahoo to IOI-RLZ
Facebook
-----
__________________
The only Verdict is Vengeance a Vendetta held as a Votive, not in Vain, for the Value and Veracity of such shall one day Vindicate the Vigilant and the Virtuous


Mav RLZ AC/DC RLZ

Reply With Quote
  #3  
Old November 2nd, 2009, 04:42 PM
jwdonahue's Avatar
jwdonahue jwdonahue is offline
Bellevue WA, USA
Click here for more information.
 
Join Date: May 2004
Location: Bellevue Washington, USA
Posts: 2,527 jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 2 Weeks 5 Days 11 h 19 m 53 sec
Reputation Power: 603
#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/

Reply With Quote
  #4  
Old November 2nd, 2009, 06:11 PM
Nyan Nyan is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2009
Posts: 230 Nyan Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 3 Days 20 h 22 m 46 sec
Reputation Power: 0
#define OutputResults( res ) printf( res )
Someone calls like:
OutputResults("%x%x%x");
Comments on this post
clifford agrees: Bad practice. But then again the question made no sense in any case; an answer should not have even
been attempted.

Reply With Quote
  #5  
Old November 2nd, 2009, 09:08 PM
clifford's Avatar
clifford clifford is offline
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Aug 2003
Location: UK
Posts: 3,748 clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Weeks 3 Days 6 h 21 m 41 sec
Reputation Power: 1167
Quote:
Originally Posted by dspgro
Is there a way to output results in form of values from a macro ?
What does that even mean!?
Comments on this post
IOI-RLZ agrees: exactly my first impression

Reply With Quote
  #6  
Old November 3rd, 2009, 02:01 PM
dspgro dspgro is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2009
Posts: 46 dspgro Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 6 h 33 m 2 sec
Reputation Power: 0
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.

Reply With Quote
  #7  
Old November 3rd, 2009, 02:47 PM
jwdonahue's Avatar
jwdonahue jwdonahue is offline
Bellevue WA, USA
Click here for more information.
 
Join Date: May 2004
Location: Bellevue Washington, USA
Posts: 2,527 jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 2 Weeks 5 Days 11 h 19 m 53 sec
Reputation Power: 603
You are obviously confused. Perhaps you could give an example of how you want to actually use value()?

Reply With Quote
  #8  
Old November 3rd, 2009, 03:17 PM
sizablegrin's Avatar
sizablegrin sizablegrin is offline
Crab
Click here for more information.
 
Join Date: Jun 2005
Posts: 5,778 sizablegrin User rank is General 53rd Grade (Above 100000 Reputation Level)sizablegrin User rank is General 53rd Grade (Above 100000 Reputation Level)sizablegrin User rank is General 53rd Grade (Above 100000 Reputation Level)sizablegrin User rank is General 53rd Grade (Above 100000 Reputation Level)sizablegrin User rank is General 53rd Grade (Above 100000 Reputation Level)sizablegrin User rank is General 53rd Grade (Above 100000 Reputation Level)sizablegrin User rank is General 53rd Grade (Above 100000 Reputation Level)sizablegrin User rank is General 53rd Grade (Above 100000 Reputation Level)sizablegrin User rank is General 53rd Grade (Above 100000 Reputation Level)sizablegrin User rank is General 53rd Grade (Above 100000 Reputation Level)sizablegrin User rank is General 53rd Grade (Above 100000 Reputation Level)sizablegrin User rank is General 53rd Grade (Above 100000 Reputation Level)sizablegrin User rank is General 53rd Grade (Above 100000 Reputation Level)sizablegrin User rank is General 53rd Grade (Above 100000 Reputation Level)sizablegrin User rank is General 53rd Grade (Above 100000 Reputation Level)sizablegrin User rank is General 53rd Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 3 Weeks 10 h 2 m 46 sec
Reputation Power: 4482
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

Reply With Quote
  #9  
Old November 3rd, 2009, 04:18 PM
clifford's Avatar
clifford clifford is offline
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Aug 2003
Location: UK
Posts: 3,748 clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Weeks 3 Days 6 h 21 m 41 sec
Reputation Power: 1167
Quote:
Originally Posted by dspgro
We know that a Macro can be of multiple lines. Suppose i want a function a to be a macro which just returns a value of pointer.

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:
Originally Posted by dspgro
Something like

int value(int x)
{
int *ptr;
ptr=x;
return *ptr;
}
Very bad example, assigning an integer to a pointer - without so much as a cast!? No code tags, and trivially reduced to a simple expression:
Code:
*((int*)x) ;
, so you might have:
Code:
int z = [code]int z = *((int*)(x)) ;
in the rare circumstances when such an expression might be useful; are you sure that code does what you intended!?

Quote:
Originally Posted by dspgro
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
Damn right it won't work. Macros are not functions. The preprocessor merely replaces a macro with the content - like a kind of parametric search and replace. The resultant code must be valid C for it to compile. In your attempt, you will have attempted to declare a variable, which in C89/90 is only valid at the top of a statement-block, and you have a return, which will not return from the macro, but rather from the function in which the macro is inserted. Also you have omitted the \ on the first line - creating an empty macro and a statement block in the middle of nowhere useful.

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))
Now remembering that the preprocessor merely performs text replacement when you write:
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.

Reply With Quote
  #10  
Old November 3rd, 2009, 05:04 PM
dspgro dspgro is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2009
Posts: 46 dspgro Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 6 h 33 m 2 sec
Reputation Power: 0
Quote:
Originally Posted by jwdonahue
You are obviously confused. Perhaps you could give an example of how you want to actually use value()?


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.

Reply With Quote
  #11  
Old November 3rd, 2009, 05:09 PM
jwdonahue's Avatar
jwdonahue jwdonahue is offline
Bellevue WA, USA
Click here for more information.
 
Join Date: May 2004
Location: Bellevue Washington, USA
Posts: 2,527 jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 2 Weeks 5 Days 11 h 19 m 53 sec
Reputation Power: 603
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.

Reply With Quote
  #12  
Old November 3rd, 2009, 05:14 PM
dspgro dspgro is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2009
Posts: 46 dspgro Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 6 h 33 m 2 sec
Reputation Power: 0
Quote:
Originally Posted by sizablegrin
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.



Sure i will make a point to read them before my next post.

Reply With Quote
  #13  
Old November 3rd, 2009, 05:19 PM
dspgro dspgro is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2009
Posts: 46 dspgro Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 6 h 33 m 2 sec
Reputation Power: 0
Quote:
Originally Posted by jwdonahue
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.


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.

Reply With Quote
  #14  
Old November 3rd, 2009, 05:26 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 7th Plane (8000 - 8499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 8,350 Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 4 Days 16 h 37 m 13 sec
Reputation Power: 2538
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

Reply With Quote
  #15  
Old November 3rd, 2009, 05:41 PM
jwdonahue's Avatar
jwdonahue jwdonahue is offline
Bellevue WA, USA
Click here for more information.
 
Join Date: May 2004
Location: Bellevue Washington, USA
Posts: 2,527 jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 2 Weeks 5 Days 11 h 19 m 53 sec
Reputation Power: 603
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Macro


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek