Gmail Calendar Documents Web Sites more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Macro function syntax
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  14 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Ann O'Nymous  
View profile  
 More options Jul 28, 6:52 pm
Newsgroups: comp.lang.c
From: Ann O'Nymous <nob...@nowhere.com>
Date: Wed, 28 Jul 2010 18:52:34 -0400
Local: Wed, Jul 28 2010 6:52 pm
Subject: Macro function syntax
I have a macro function that's like the following:

int function2(int*,int,int,int);

#define xyz(a,b,c) {\
int x[100],i; \
for(i=0;i<c;i++) x[i] = something(a,b,c); \
function2(x,a,b,c); \

}

where "something" is a mathematical expression of a,b and c, and
"function2" is a function returning int.

Calls like:

  xyz(1,2,3);

work fine, other than the return value of "function2" gets thrown away.

Now, if I want to do this:

int jkl;
...
jkl = xyz(1,2,3);
...

where "jkl" is to get that value returned by "function2", how do I do
it? I'm having trouble with the syntax.

I want things to expand where:

  jkl = xyz(p,q,r);

expands equivalently to:
{
int x[100],i;
for(i=0;i<r;i++) x[i] = something(p,q,r);
jkl = function2(x,p,q,r);


    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
pete  
View profile  
 More options Jul 28, 7:00 pm
Newsgroups: comp.lang.c
From: pete <pfil...@mindspring.com>
Date: Wed, 28 Jul 2010 19:00:09 -0400
Local: Wed, Jul 28 2010 7:00 pm
Subject: Re: Macro function syntax

#define xyz(a,b,c,jkl) {\

--
pete


    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ann O'Nymous  
View profile  
 More options Jul 29, 9:09 am
Newsgroups: comp.lang.c
From: Ann O'Nymous <nob...@nowhere.com>
Date: Thu, 29 Jul 2010 09:09:35 -0400
Local: Thurs, Jul 29 2010 9:09 am
Subject: Re: Macro function syntax
On 7/28/2010 7:00 PM, pete wrote:

> #define xyz(a,b,c,jkl) {\

Thanks, but I can't actually do this.  I simplified my problem, and am
actually using a variadic argument list.

    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ben Bacarisse  
View profile  
 More options Jul 29, 9:50 am
Newsgroups: comp.lang.c
From: Ben Bacarisse <ben.use...@bsb.me.uk>
Date: Thu, 29 Jul 2010 14:50:06 +0100
Local: Thurs, Jul 29 2010 9:50 am
Subject: Re: Macro function syntax

Ann O'Nymous <nob...@nowhere.com> writes:
> On 7/28/2010 7:00 PM, pete wrote:

>> #define xyz(a,b,c,jkl) {\

> Thanks, but I can't actually do this.  I simplified my problem, and am
> actually using a variadic argument list.

Without the actual code (including the full range of calls that might
be made) lots of answers may be wasted.

There is a big picture question that might help to have answered.  Why
not use a function?  The answer may be that you can't re-write the two
called functions to take a va_list, but that as least rules out the
obvious solution.

--
Ben.


    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ann O'Nymous  
View profile  
 More options Jul 29, 1:06 pm
Newsgroups: comp.lang.c
From: Ann O'Nymous <nob...@nowhere.com>
Date: Thu, 29 Jul 2010 13:06:00 -0400
Subject: Re: Macro function syntax
On 7/29/2010 9:50 AM, Ben Bacarisse wrote:

OK. I simplified things so I'd get a generic answer so I'd know what to
do next time.  This is what I have at the moment:

void buildlist(unsigned int *,...);
int zlink(int(int,...),int,unsigned int *);

#define LINKX(routine,flags,...)\
   {unsigned int list[MAXARGS];\
   buildlist(list,__VA_ARGS__);\
   j = zlink((routine),(flags),list);\
   }

"routine" is a function (one of many) called by zlink with a specialized
parameter list in array list, and the functions cannot be changed.  It
returns int.

zlink() cannot be changed.

The "j=zlink()" is a hack to get the return value out.

Calls are like this:

int returnvalue1,returnvalue2;
int j;
int module1(int,...),module2(int,...);
...
LINKX(module1,flag,1,2,3);
returnvalue1=j;
...
LINKX(module2,flag2,4,5,6,7,8,9);
returnvalue2=j;
...
I'd like to eliminate the "j=zlink() hack and change things so calls are
like this:

returnvalue1 = LINKX(module1,flag,1,2,3);


    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
David Resnick  
View profile  
 More options Jul 29, 1:40 pm
Newsgroups: comp.lang.c
From: David Resnick <lndresn...@gmail.com>
Date: Thu, 29 Jul 2010 10:40:14 -0700 (PDT)
Local: Thurs, Jul 29 2010 1:40 pm
Subject: Re: Macro function syntax
On Jul 29, 1:06 pm, Ann O'Nymous <nob...@nowhere.com> wrote:

Why not put the return value as a LINKX argument before the valist?
as in
#define LINKX(routine, flags, returnvar,...)\
...
     returnvar = zlink((routine),(falgs),list);

Usage:
LINKX(module2,flag2,returnvalue2,4,5,6,7,8,9);

This is what pete was suggesting I'd guess, why did you think it a bad
idea?

-David


    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Kenneth Brody  
View profile  
 More options Jul 29, 1:41 pm
Newsgroups: comp.lang.c
From: Kenneth Brody <kenbr...@spamcop.net>
Date: Thu, 29 Jul 2010 13:41:19 -0400
Local: Thurs, Jul 29 2010 1:41 pm
Subject: Re: Macro function syntax
On 7/29/2010 9:09 AM, Ann O'Nymous wrote:

[... putting back snipped context ...]

Place the "return value" as the first parameter, rather than the last.

Now, this goes against many people's coding standards, as it hides the fact
that it's going to change the value of that first parameter, but it does "work".

You could "unhide" this a little, by putting:

     (*retval) = function2(x,p,q,r);

and passing:

     xyz(&jkl,1,2,3);

It looks ugly, but again, it "works".

A minor(?) nit here is the "extraneous" semicolon in this syntax.  Consider:

     if ( foo )
         xyz(&jkl,1,2,3);
     else
         xyz(&jkl,4,5,6);

This will expand to:

     if ( foo )
        {
        your macro
        };
     else
        {
        your macro
        };

which will generate a syntax error.  (Or, worse, not generate one if this is
a nested if.)

I believe the "standard" way of handling this is to put the entire code
block in a do...while(0) loop:

     #define xyz(retval,x,y,z) do { \
         ... your code ...          \
         } while (0)

Note the lack of a terminating semicolon on the while.  This will be
supplied by the semicolon in the "call" to the macro.

--
Kenneth Brody


    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ben Bacarisse  
View profile  
 More options Jul 29, 2:07 pm
Newsgroups: comp.lang.c
From: Ben Bacarisse <ben.use...@bsb.me.uk>
Date: Thu, 29 Jul 2010 19:07:50 +0100
Local: Thurs, Jul 29 2010 2:07 pm
Subject: Re: Macro function syntax
Ann O'Nymous <nob...@nowhere.com> writes:

<snip>

What other bits of C99 can you use?  If you can use compound literals
you can turn the macro into a expression provided that you can change
buildlist to return a pointer to its list:

  #define LINKX(routine, flags, ...)\
     zlink((routine), (flags),\
           buildlist((unsigned int[MAXARGS]){0}, __VA_ARGS__));

Alternatively, you might be able to share the temporary array:

  #define LINKX(routine, flags, ...)\
    (buildlist(shared_list, __VA_ARGS__),\
     zlink((routine), (flags), shared_list))

You then need to define shared_array in the containing block.  If
buildlist can use a static array and return that you could write:

  #define LINKX(routine, flags, ...)\
     zlink((routine), (flags), buildlist(__VA_ARGS__));

There is also a gcc-specific solution using ({}).

--
Ben.


    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ann O'Nymous  
View profile  
 More options Jul 29, 2:14 pm
Newsgroups: comp.lang.c
From: Ann O'Nymous <nob...@nowhere.com>
Date: Thu, 29 Jul 2010 14:14:05 -0400
Local: Thurs, Jul 29 2010 2:14 pm
Subject: Re: Macro function syntax
On 7/29/2010 1:40 PM, David Resnick wrote:

> Why not put the return value as a LINKX argument before the valist?
...
> This is what pete was suggesting I'd guess, why did you think it a bad
> idea?

It's not a bad idea, and it's probably what I'll be doing.  It just
seems that there has to be a better way, a C function macro should be
able to act like a function.  I just want the code to look like a
function call, since the called routines are often functions.

However, some of the time, they are not functions, or the function
return value is ignored, meaning the extra parameter is not really
used.

So, is there a way to rewrite LINKX to be able to use the syntax
x=LINKX(module,flag,foo); or
LINKX(module,flag,foo);  ?
If not, I'll add the extra parameter.

Also to Kenneth B, I am aware of the "do {...} while (0)" issue and may
do that if I have calls like you mention.  In fact, I'm sure I will have
to do that.
Also is "x = do {...} while (0);" valid C syntax? (if so, what does x
get set to?)


    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Keith Thompson  
View profile  
 More options Jul 29, 3:55 pm
Newsgroups: comp.lang.c
From: Keith Thompson <ks...@mib.org>
Date: Thu, 29 Jul 2010 12:55:38 -0700
Local: Thurs, Jul 29 2010 3:55 pm
Subject: Re: Macro function syntax
Ann O'Nymous <nob...@nowhere.com> writes:
> On 7/29/2010 1:40 PM, David Resnick wrote:
>> Why not put the return value as a LINKX argument before the valist?
> ...
>> This is what pete was suggesting I'd guess, why did you think it a bad
>> idea?

> It's not a bad idea, and it's probably what I'll be doing.  It just
> seems that there has to be a better way, a C function macro should be
> able to act like a function.  I just want the code to look like a
> function call, since the called routines are often functions.

[...]

There is no such thing as a "function macro" or a "macro function" in C.
There are "function-like macros", but those are merely macros with
parameters.

Keep in mind that macro expansion is almost purely textual, and applies
only to the macro invocation itself.  For example, given:

    x = MY_MACRO(y, z);

the expansion of MY_MACRO can't do anything with x.

--
Keith Thompson (The_Other_Keith) ks...@mib.org  <http://www.ghoti.net/~kst>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"


    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Shao Miller  
View profile  
 More options Jul 29, 8:34 pm
Newsgroups: comp.lang.c
From: Shao Miller <sha0.mil...@gmail.com>
Date: Thu, 29 Jul 2010 17:34:26 -0700 (PDT)
Local: Thurs, Jul 29 2010 8:34 pm
Subject: Re: Macro function syntax
#define MAXARGS 10
typedef unsigned int zlink_args[MAXARGS];

int zlink(int(int, ...), int, unsigned int *);
int zlink(int bar(int, ...), int flags, unsigned int *list) {
  return 12;

}

unsigned int *buildlist(zlink_args list, ...) {
  /* ... */
  return list;

}

int foo(int bar, ...) {
  return 5;

}

#define LINKX(routine,flags,list, ...) \
  (zlink((routine),(flags),buildlist((list), __VA_ARGS__)))

int main(void) {
  zlink_args mylist;
  int w, x, y, z;

  w = x = y = z = 10;
  return LINKX(foo, (1 << 3) & (1 << 4), mylist, w, x, y, z);


    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Shao Miller  
View profile  
 More options Jul 29, 8:41 pm
Newsgroups: comp.lang.c
From: Shao Miller <sha0.mil...@gmail.com>
Date: Thu, 29 Jul 2010 17:41:52 -0700 (PDT)
Local: Thurs, Jul 29 2010 8:41 pm
Subject: Re: Macro function syntax
On Jul 29, 8:34 pm, Shao Miller <sha0.mil...@gmail.com> wrote:

>   return LINKX(foo, (1 << 3) & (1 << 4), mylist, w, x, y, z);

'|', not '&'. ;)

    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ann O'Nymous  
View profile  
 More options Jul 30, 11:39 am
Newsgroups: comp.lang.c
From: Ann O'Nymous <nob...@nowhere.com>
Date: Fri, 30 Jul 2010 11:39:48 -0400
Local: Fri, Jul 30 2010 11:39 am
Subject: Re: Macro function syntax
On 7/29/2010 8:34 PM, Shao Miller wrote:

I'll probably do something like this, although I want to hide the
zlink_args stuff in the macro.  Thanks.

    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Shao Miller  
View profile  
 More options Jul 30, 4:42 pm
Newsgroups: comp.lang.c
From: Shao Miller <sha0.mil...@gmail.com>
Date: Fri, 30 Jul 2010 13:42:44 -0700 (PDT)
Local: Fri, Jul 30 2010 4:42 pm
Subject: Re: Macro function syntax
On Jul 30, 11:39 am, Ann O'Nymous <nob...@nowhere.com> wrote:
> I'll probably do something like this, although I want to hide the
> zlink_args stuff in the macro.  Thanks.

Could you at all be interested in sharing a bit more about your
requirement for building the arguments into an array that's all inside
the macro?

For example, why use a macro at all?  You could use a function that
allocates the array, populates it from arguments, then calls 'zlink'
and returns that call's result.

Also, consider a loop where we don't pass 'mylist':

enum { lim = 50 };
int i, rc, w, x, y, z;

w = x = y = z = 0;
for (i = 0; i < lim; i++) {
  rc = LINKX(foo, (1 << 3) & (1 << 4), w, x, y, z);
  if (rc) {
    /* ... */
  }

}

If you use Ben's suggestion of C99 compound literals within your
'LINKX' macro, you get nameless objects with automatic storage
duration.  When does their lifetime end?  If it's the 'for' {} body,
then what's to prevent allocating 50 of them above, as per 'lim'?
That might get expensive.

Are you trying to make minimal changes to someone else's code?  Are
you using 'setjmp()' and friends and have concerns about allocating
something like 'mylist' in a function's body?  Or maybe you don't wish
to add such an allocation to every function using the 'LINKX' macro?


    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2010 Google