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). 7 1698
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))
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
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.
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);
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.
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
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 ** This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: jpodesta |
last post by:
Hello-
I am fairly new to MS Access and would like to use some macros in .xls
in an Access Module. I have tried to do this on my own but failed to
make it work. I have included the xls macros...
|
by: Alexander Ulyanov |
last post by:
Hi all.
Is it possible to pass the whole blocks of code (possibly including
" and ,) as macro parameters?
I want to do something like:
MACRO(FOO, "Foo",
"return "Foobar";",
"foo();...
|
by: Steve Lambert |
last post by:
Hi,
I've knocked up a number of small routines to create and manipulate a linked
list of any structure. If anyone could take a look at this code and give me
their opinion and details of any...
|
by: Little |
last post by:
I have this program and I need to work on the test portion, which tests
if a Val is in the list. It returns false no matter what could you look
at the part and see what might need to be done to fix...
|
by: skinnybloke |
last post by:
Hi - I have 3 access queries which I run via 1 macro.
Each of the queries now requires 2 parameters when they the run. The
parameters are start and end dates.
I have built the parameters...
|
by: Jack Morgan |
last post by:
I got a macro like this in my code.
>
#define DECLARE_SOMEWNDSTRUCTURE(style, nums, clrref, icon, tablenum,
name,
menuname)\
static SOMEWNDSTRUCT &GetSomeWndStruct()\
{\
static SOMEWNDSTRUCT...
|
by: Max TenEyck Woodbury |
last post by:
I need a macro that will take an arbitrarily long list of arguments
where each argument needs to be passed to another macro.
Is it possible to write such a macro?
If so, could you please...
|
by: chapeau_melon |
last post by:
Hello,
I'm basicly not a programmer...
I found some C++ codes on the net that almost satisfy me needs, wich
is to communicate with an other device that sends data to me, wich I
have to receive...
|
by: =?GB2312?B?17/HvyBaaHVvLCBRaWFuZw==?= |
last post by:
Hi,
I would like to have someone comments on what's the best practice
defining error codes in C.
Here's what I think:
solution A:
using enum
pros: type safe. better for debug (some debugger...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
|
by: tracyyun |
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: NeoPa |
last post by:
Hello everyone.
I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report).
I know it can be done by selecting :...
|
by: Teri B |
last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course.
0ne-to-many. One course many roles.
Then I created a report based on the Course form and...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
|
by: isladogs |
last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, Mike...
|
by: GKJR |
last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
| |