470,596 Members | 1,594 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,596 developers. It's quick & easy.

Defining a list of macro parameters.

I would like to define a list of parameters, and then pass them to a macro.
However, the compiler (gcc) only sees one parameter, rather than expanding
the definition.
Could anyone suggest a way of making this work (using the preprocessor)?

#define MyList 1,2,3,4

#define Sum(a,b,c,d) a+b+c+d

x = Sum(MyList);

(n.b. i can pass 'MyList' to a function, but i'd rather pass it to a macro).
Jun 27 '08 #1
7 1553
On Wed, 30 Apr 2008 08:44:57 +0100, Paul wrote:
I would like to define a list of parameters, and then pass them to a
macro. However, the compiler (gcc) only sees one parameter, rather than
expanding the definition.
That's the correct behaviour. The list of macro arguments is first split,
and then expanded. Otherwise, what would you expect

#define MACRO(foo, ...) #foo foo
#define LIST 1,2,3
MACRO(LIST, 4)

to expand to? #foo doesn't expand the parameter, so it can only be
"LIST", but foo by itself would only be 1?
Could anyone suggest a way of making this work (using the preprocessor)?

#define MyList 1,2,3,4

#define Sum(a,b,c,d) a+b+c+d

x = Sum(MyList);
#define Invoke(Macro, Args) Macro Args
Invoke(Sum, (MyList))
Jun 27 '08 #2
Paul wrote:
...Could anyone suggest a way of making this work (using
the preprocessor)?

#define MyList 1,2,3,4

#define Sum(a,b,c,d) a+b+c+d

x = Sum(MyList);

(n.b. i can pass 'MyList' to a function, but i'd rather pass it to a macro).
#include <stdio.h>

#define MyList 1, 2, 3, 4
#define Sum(a,b,c,d) ((a) + (b) + (c) + (d))

#define SUM(LIST) Sum(LIST)

int main(void)
{
printf("%d\n", SUM(MyList));
return 0;
}

--
Peter
Jun 27 '08 #3
On Apr 30, 4:11*pm, Peter Nilsson <ai...@acay.com.auwrote:
Paul wrote:
...Could anyone suggest a way of making this work (using
the preprocessor)?
#define MyList *1,2,3,4
#define Sum(a,b,c,d) a+b+c+d
*x = Sum(MyList);
(n.b. i can pass 'MyList' to a function, but i'd rather pass it to a macro).

* #include <stdio.h>

* #define MyList 1, 2, 3, 4
* #define Sum(a,b,c,d) ((a) + (b) + (c) + (d))

* #define SUM(LIST) Sum(LIST)

* int main(void)
* {
* * printf("%d\n", SUM(MyList));
* * return 0;
* }

C:\tmp>cl /W4 /Ox oopsie.c
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.762
for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.

oopsie.c
oopsie.c(12) : warning C4003: not enough actual parameters for macro
'Sum'
oopsie.c(12) : error C2059: syntax error : ')'

C:\tmp>type oopsie.c
#include <stdio.h>

#define MyList 1, 2, 3, 4
#define Sum(a,b,c,d) ((a) + (b) + (c) + (d))
#define SUM(LIST) Sum(LIST)
int main(void)
{
printf("%d\n", SUM(MyList));
return 0;
}

My effort:
#include <stdio.h>
int main(void) {puts("10");return 0;}

It might be possible to accomplish the OP's task, but this one is even
worse than the sizeof without sizeof as far as brain dead requests go.
Someone needs to get "The C Puzzle Book" of the professor's desk and
give him a real textbook.
IMO-YMMV

P.S.
Let's head this one off at the pass:

6.11: I came across some "joke" code containing the "expression"
5["abcdef"] . How can this be legal C?

A: Yes, Virginia, array subscripting is commutative in C. This
curious fact follows from the pointer definition of array
subscripting, namely that a[e] is identical to *((a)+(e)), for
*any* two expressions a and e, as long as one of them is a
pointer expression and one is integral. This unsuspected
commutativity is often mentioned in C texts as if it were
something to be proud of, but it finds no useful application
outside of the Obfuscated C Contest (see question 20.36).

References: Rationale Sec. 3.3.2.1; H&S Sec. 5.4.1 p. 124,
Sec. 7.4.1 pp. 186-7.

Jun 27 '08 #4
Harald and Peter: Thank you for your solutions, cheers!

"user923005" <dc*****@connx.comwrote...
>My effort:
#include <stdio.h>
int main(void) {puts("10");return 0;}
>It might be possible to accomplish the OP's task, but this one is even
worse than the sizeof without sizeof as far as brain dead requests go.
Someone needs to get "The C Puzzle Book" of the professor's desk and
give him a real textbook.
IMO-YMMV

user, thankyou for your contribution, however the line puts("10") does not
work properly for my intended application (which i simplified for posting):

#define GreenLed ( GPIOB, 10, OutputOpenDrain, InvertedOut)
#define InvertedOut &=~,|=
#define Port(Function, Name) Function Name
#define TurnOn(Port,Pin,Type,On,Off) Port->ODR On Bit(Pin)
Port(TurnOn,GreenLed);

Jun 27 '08 #5
On May 1, 2:23*am, "Paul" <-wrote:
Harald and Peter: Thank you for your solutions, cheers!

"user923005" <dcor...@connx.comwrote...
My effort:
#include <stdio.h>
int main(void) {puts("10");return 0;}
It might be possible to accomplish the OP's task, but this one is even
worse than the sizeof without sizeof as far as brain dead requests go.
Someone needs to get "The C Puzzle Book" of the professor's desk and
give him a real textbook.

IMO-YMMV

user, thankyou for your contribution, however the line puts("10") does not
work properly for my intended application (which i simplified for posting):

#define GreenLed ( GPIOB, 10, OutputOpenDrain, InvertedOut)
#define InvertedOut &=~,|=
#define Port(Function, Name) Function Name
#define TurnOn(Port,Pin,Type,On,Off) Port->ODR On Bit(Pin)
Port(TurnOn,GreenLed);
Don't use macros. Use functions.
Jun 27 '08 #6
user923005 wrote:
Peter Nilsson <ai...@acay.com.auwrote:
#include <stdio.h>

#define MyList 1, 2, 3, 4
#define Sum(a,b,c,d) ((a) + (b) + (c) + (d))

#define SUM(LIST) Sum(LIST)

int main(void)
{
printf("%d\n", SUM(MyList));
return 0;
}

C:\tmp>cl /W4 /Ox oopsie.c
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.762
for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.

oopsie.c
oopsie.c(12) : warning C4003: not enough actual parameters for macro
'Sum'
oopsie.c(12) : error C2059: syntax error : ')'
Don't let me stop you filing a bug report with Microsoft.

--
Peter
Jun 27 '08 #7
Paul wrote:
>
.... snip ....
>
user, thankyou for your contribution, however the line puts("10")
does not work properly for my intended application (which i
simplified for posting):
Did you #include <stdio.h>?

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
** Posted from http://www.teranews.com **
Jun 27 '08 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by jpodesta | last post: by
3 posts views Thread by Alexander Ulyanov | last post: by
6 posts views Thread by Steve Lambert | last post: by
5 posts views Thread by Little | last post: by
28 posts views Thread by Jack Morgan | last post: by
4 posts views Thread by Max TenEyck Woodbury | last post: by
5 posts views Thread by =?GB2312?B?17/HvyBaaHVvLCBRaWFuZw==?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.