Why this MACRO does not work?

K

Karim Thapa

I defined a MACRO 'removebrace' as following

#define removebrace(x) x

main() {

...

MyFunc(1, 2, removebrace("hello", 5, 6));


....

}

I expect this statement to expand in

MyFunc(1, 2, "hello", 5, 6);

I use MSVC. I can see in disassembly that only three values are pushed
into the stack before calling function 'MyFunc', that is pointer to
string "hello", 2 and 1, instead of all five arguments.

However, if I do not use MACRO 'removebrace', all five values are
pushed into the stack and it works as expected.

I can't understand why using MACRO 'removebrace' creates problem.

Thanks for any help.

Karim
 
G

Grumble

Karim said:
I defined a MACRO 'removebrace' as following

#define removebrace(x) x

main() {

..

MyFunc(1, 2, removebrace("hello", 5, 6));

According to the Unicode Consortium code charts, '(' and ')' are
called, respectively, left (or opening) and right (or closing)
parenthesis.

{' and '}' are called braces or curly brackets.
I expect this statement to expand in

MyFunc(1, 2, "hello", 5, 6);

I use MSVC.

My compiler says:

test.c:5: macro `removebrace' used with too many (3) args
 
V

Vijay Kumar R Zanvar

Karim Thapa said:
I defined a MACRO 'removebrace' as following

#define removebrace(x) x

main() {

..

MyFunc(1, 2, removebrace("hello", 5, 6));

As was previously mentioned, your macro expects only one argument,
where as you passed three! You can think of this in the lines of
a function's signature. You pass only that much of arguments as
declared by the prototype.

Last time I wrongly gave a solution to your problem. This time,
again, I propose a solution. Define your macro as:

#include <stdio.h>
#include <stdlib.h>


/*
* "info gcc" pages mention that C99 allows variable
* length macros
*/

#ifdef __STDC__
#define removebrace( x, ... ) ( x ), __VA_ARGS__
#else
#define removebrace( x, y, z ) ( x ), ( y ), ( z ) /* useless */
#endif

int
main ( void )
{
MyFunc(1, 2, removebrace("hello", 5, 6));
return EXIT_SUCCESS;
}

Compile as

$ gcc a.c -std=c99

[..]
 
V

Vijay Kumar R Zanvar

Karim Thapa said:
I defined a MACRO 'removebrace' as following

#define removebrace(x) x

main() {

..

MyFunc(1, 2, removebrace("hello", 5, 6));

As was previously mentioned, your macro expects only one argument,
where as you passed three! You can think of this in the lines of
a function's signature. You pass only that much of arguments as
declared by the prototype.

Last time I wrongly gave a solution to your problem. This time,
again, I propose a solution. Define your macro as:

#include <stdio.h>
#include <stdlib.h>


/*
* "info gcc" pages mention that C99 allows variable
* length macros
*/

#ifdef __STDC__
#define removebrace( x, ... ) ( x ), __VA_ARGS__
#else
#define removebrace( x, y, z ) ( x ), ( y ), ( z ) /* useless */
#endif

int
main ( void )
{
MyFunc(1, 2, removebrace("hello", 5, 6));
return EXIT_SUCCESS;
}

Compile as

$ gcc a.c -std=c99

[..]
 
R

Richard Bos

I defined a MACRO 'removebrace' as following

#define removebrace(x) x

You've already had about half a gross of answers to this question, all
of them indicating that this macro definition doesn't do anything much.
If you didn't like those answer, tough; re-posting the question won't
make the macro suddenly work.

Richard
 
H

Hans-Bernhard Broeker

In comp.lang.c.moderated Karim Thapa said:
I defined a MACRO 'removebrace' as following
#define removebrace(x) x
main() {

MyFunc(1, 2, removebrace("hello", 5, 6));

Your compiler should've complained about this --- you defined a macro
with one argument, but you're calling it with *three*. This is a bug
in your code. Since parentheses are an integral part of the syntax
of a macro call, you cannot just remove them.

To see what exactly happens, get your compiler to show you a
preprocessed version of your source.
 
D

Dave Hansen

I defined a MACRO 'removebrace' as following

#define removebrace(x) x

main() {

..

MyFunc(1, 2, removebrace("hello", 5, 6));


...

}

I expect this statement to expand in

MyFunc(1, 2, "hello", 5, 6);

Why? You've defined 'removebrace' to take one argument, yet you give
it three. I'm not sure what the preprocessor is supposed to do with
it, though I suspect it's undefined. It appears your compiler ignores
the second and third parameters.

Perhaps you meant

MyFunc(1, 2, removebrace(("hello", 5, 6)));

which at least has just one argument. But it expands to

MyFunc(1, 2, ("hello", 5, 6));

which is probably not what you want either (still just three
parameters, the third being 6 instead of "hello").

What will work is

#define removebrace(x,y,z) x,y,z

along with your original function call. But I'm guessing that's not
what you want, either.

Regards,

-=Dave
 
K

Keith Thompson

I defined a MACRO 'removebrace' as following

#define removebrace(x) x

main() {
MyFunc(1, 2, removebrace("hello", 5, 6));
}

I expect this statement to expand in

MyFunc(1, 2, "hello", 5, 6);

I'd expect it to fail to compiler. Your removebrace() macro takes one
argument; you've given it three.

You can call it like this:

MyFunc(1, 2, removebrace(("hello", 5, 6)));

This invokes removebrace() with a single argument: ("hello", 5, 6).

(BTW, "main()" is better writen as "int main(void)").
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top