niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Programming » C++
Variable Number of Arguments in Macro
Post new topic   Reply to topic Page 1 of 1 [11 Posts] View previous topic :: View next topic
Author Message
Praveen.Kumar.SP@gmail.co
*nix forums beginner


Joined: 29 Jun 2006
Posts: 1

PostPosted: Thu Jun 29, 2006 2:05 pm    Post subject: Variable Number of Arguments in Macro Reply with quote

Hi

Could anyone solve the problem for the code below

The Code:

#include "stdio.h"
#include "iostream.h"

void Temp( int a, char* str,...)
{
//code to handle the arguments
}

#define MYPRINT(_x_) printf _x_
#define MYPRINT1(_x_) Temp( 10,_x_)

int main()
{
MYPRINT(("This is a test for multiple argument %s
%d",__FILE__,__LINE__));
MYPRINT1(("This is a test for multiple argument %s
%d",__FILE__,__LINE__));

return 0;
}

Problem:

In the first macro I am able to get the result as expected from the
printf where as in the second case my parameters are not properly
passed to the function Temp. Could anyone of you tell me why i am not
abel to use the macro to pass parameter to a function with some
mandatory number of parameter and variable number of parameter?

Is there any way that i can paa the parameter as i expected?how?


Thanks
praveen
Back to top
Victor Bazarov
*nix forums Guru


Joined: 07 Apr 2005
Posts: 3949

PostPosted: Thu Jun 29, 2006 2:10 pm    Post subject: Re: Variable Number of Arguments in Macro Reply with quote

Praveen.Kumar.SP@gmail.com wrote:
Quote:
Could anyone solve the problem for the code below

The Code:

#include "stdio.h"
#include "iostream.h"

void Temp( int a, char* str,...)
{
//code to handle the arguments
}

#define MYPRINT(_x_) printf _x_
#define MYPRINT1(_x_) Temp( 10,_x_)

int main()
{
MYPRINT(("This is a test for multiple argument %s
%d",__FILE__,__LINE__));
MYPRINT1(("This is a test for multiple argument %s
%d",__FILE__,__LINE__));

return 0;
}

Problem:

In the first macro I am able to get the result as expected from the
printf where as in the second case my parameters are not properly
passed to the function Temp. Could anyone of you tell me why i am not
abel to use the macro to pass parameter to a function with some
mandatory number of parameter and variable number of parameter?

Because when the macro MYPRINT1 is substituted you get

Temp( 10,("This is..","filename.ext",123));

The extra set of parentheses around the arguments makes it a single
expression with two operators comma instead of part of the list of
arguments to the 'Temp' function. BTW, you get 123 where 'char*'
is expected. It's most likely undefined behaviour.

Quote:
Is there any way that i can paa the parameter as i expected?how?

You most likely cannot. See if your compiler supports "variadic
macros" (macros with ellipsis).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Back to top
Alf P. Steinbach
*nix forums Guru


Joined: 09 Mar 2005
Posts: 1855

PostPosted: Thu Jun 29, 2006 2:22 pm    Post subject: Re: Variable Number of Arguments in Macro Reply with quote

* Praveen.Kumar.SP@gmail.com:
Quote:

Could anyone solve the problem for the code below

"The" problem? I count a multitude of problems. Which one?


Quote:
The Code:

#include "stdio.h"

Use the <headername> form instead of "headername" for standard headers.
That way you avoid picking up a header with the same name in a local
directory.


Quote:
#include "iostream.h"

This is not a standard header, and won't compile with e.g. Visual C++
7.1 or better. Use <iostream> instead. <iostream> is a standard header.


Quote:
void Temp( int a, char* str,...)

The second argument should be declared as

char const* str

unless you want the function Temp to be able to modify the contents of
'str'.

The ellipsis '...' should generally not be used in C++ code, because
it's /dangerous/ (not typesafe) and /limited/ (no non-POD objects);
there are much better typesafe solutions.


Quote:
{
//code to handle the arguments
}

#define MYPRINT(_x_) printf _x_
#define MYPRINT1(_x_) Temp( 10,_x_)

Generally it's not a good idea to use macros. See this group's FAQ and
Bjarne Stroustrup's C++ FAQ for reasons why.


Quote:
int main()
{
MYPRINT(("This is a test for multiple argument %s
%d",__FILE__,__LINE__));
MYPRINT1(("This is a test for multiple argument %s
%d",__FILE__,__LINE__));

return 0;
}

Problem:

In the first macro I am able to get the result as expected from the
printf where as in the second case my parameters are not properly
passed to the function Temp. Could anyone of you tell me why i am not
abel to use the macro to pass parameter to a function with some
mandatory number of parameter and variable number of parameter?

The second macro invocation does not work because it expands to

Temp( 10, ("some text",__FILE__,__LINE__));

which is syntactically invalid.


Quote:
Is there any way that i can paa the parameter as i expected?how?

No, not as you expected.

A solution depends on what you want to achieve. Obviously it's not
what's illustrated by your code, because that could be much more easily
achieved by calling Temp directly without the macro. In other words,
you have illustrated a flawed solution to some problem, instead of the
problem itself -- to get help with that problem, explain it.

I suspect, though, that it has to do with logging or tracing?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Back to top
Victor Bazarov
*nix forums Guru


Joined: 07 Apr 2005
Posts: 3949

PostPosted: Thu Jun 29, 2006 2:30 pm    Post subject: Re: Variable Number of Arguments in Macro Reply with quote

Alf P. Steinbach wrote:
Quote:
* Praveen.Kumar.SP@gmail.com:
[..]

The second macro invocation does not work because it expands to

Temp( 10, ("some text",__FILE__,__LINE__));

Really? You mean __FILE__ and __LINE__ do not get substituted?
Why? Have you tried it?

Quote:
which is syntactically invalid.

Why is it invalid?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Back to top
Alf P. Steinbach
*nix forums Guru


Joined: 09 Mar 2005
Posts: 1855

PostPosted: Thu Jun 29, 2006 2:46 pm    Post subject: Re: Variable Number of Arguments in Macro Reply with quote

* Victor Bazarov:
Quote:
Alf P. Steinbach wrote:
* Praveen.Kumar.SP@gmail.com:
[..]
The second macro invocation does not work because it expands to

Temp( 10, ("some text",__FILE__,__LINE__));

Really?

Yep.


Quote:
You mean __FILE__ and __LINE__ do not get substituted?

No, I haven't written that; the result is churned once more through the
macro substitution machinery, and so on.

Understanding this becomes important when you have code like

#include <iostream>

#define VB( x ) #x

int main()
{
std::cout << VB(__FILE__) << std::endl;
}

where the macro invocation expands to

#__FILE__

which in the next round becomes

"__FILE__"

which results in the output of the string "__FILE__", not the source
code file name.


Quote:
Why?

Because that's how macros work; look it up in your favorite C++ textbook.


Quote:
Have you tried it?

No.


Quote:
which is syntactically invalid.

Why is it invalid?

There you caught me. Wink It's not syntactically but semantically
invalid; sorry for the typo. The type of the comma expression is 'int',
whereas the Temp function requires a char* as second argument.


--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Back to top
Victor Bazarov
*nix forums Guru


Joined: 07 Apr 2005
Posts: 3949

PostPosted: Thu Jun 29, 2006 3:11 pm    Post subject: Re: Variable Number of Arguments in Macro Reply with quote

Alf P. Steinbach wrote:
Quote:
* Victor Bazarov:
Alf P. Steinbach wrote:
* Praveen.Kumar.SP@gmail.com:
[..]
The second macro invocation does not work because it expands to

Temp( 10, ("some text",__FILE__,__LINE__));

Really?

Yep.


[...about using the # operator...]

Why?

Because that's how macros work; look it up in your favorite C++
textbook.

Have you tried it?

No.

Well, do, then.

#define M(x) printf("%s", x)
#include <stdio.h>
int main()
{
M((__FILE__));
}

And then turn to *your* favourite C++ textbook.

Quote:
which is syntactically invalid.

Why is it invalid?

There you caught me. Wink It's not syntactically but semantically
invalid; sorry for the typo. The type of the comma expression is
'int', whereas the Temp function requires a char* as second argument.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Back to top
Alf P. Steinbach
*nix forums Guru


Joined: 09 Mar 2005
Posts: 1855

PostPosted: Thu Jun 29, 2006 3:22 pm    Post subject: Re: Variable Number of Arguments in Macro Reply with quote

* Victor Bazarov:
Quote:
Alf P. Steinbach wrote:
* Victor Bazarov:
Alf P. Steinbach wrote:
* Praveen.Kumar.SP@gmail.com:
[..]
The second macro invocation does not work because it expands to

Temp( 10, ("some text",__FILE__,__LINE__));
Really?
Yep.


[...about using the # operator...]

No, what you completely snipped was about macro expansion.


Quote:
Why?
Because that's how macros work; look it up in your favorite C++
textbook.

Have you tried it?
No.

Well, do, then.

#define M(x) printf("%s", x)
#include <stdio.h
int main()
{
M((__FILE__));
}

And then turn to *your* favourite C++ textbook.

That's not an example of anything discussed previously, and, since I
don't think you don't know that: I resent that kind of discussion technique.

It doesn't remove the egg on your face. :-)

For that you need to employ a strong egg-remover, not a context-remover.


--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Back to top
Ron Natalie
*nix forums Guru


Joined: 21 Feb 2005
Posts: 461

PostPosted: Thu Jun 29, 2006 11:38 pm    Post subject: Re: Variable Number of Arguments in Macro Reply with quote

Praveen.Kumar.SP@gmail.com wrote:
Quote:
Hi

Could anyone solve the problem for the code below

C now has varadic macros. C++ doesn't (nor does it

support overloading of macros).
Back to top
Frederick Gotham
*nix forums Guru


Joined: 09 Jun 2006
Posts: 502

PostPosted: Thu Jun 29, 2006 11:44 pm    Post subject: Re: Variable Number of Arguments in Macro Reply with quote

Ron Natalie posted:


Quote:
C now has varadic macros. C++ doesn't (nor does it
support overloading of macros).


Not really, given that nobody uses C99.


--

Frederick Gotham
Back to top
Ian
*nix forums Guru


Joined: 16 Aug 2005
Posts: 1615

PostPosted: Fri Jun 30, 2006 12:30 am    Post subject: Re: Variable Number of Arguments in Macro Reply with quote

Frederick Gotham wrote:
Quote:
Ron Natalie posted:



C now has varadic macros. C++ doesn't (nor does it
support overloading of macros).



Not really, given that nobody uses C99.

I do, so does my choice of OS.


--
Ian Collins.
Back to top
ax
*nix forums beginner


Joined: 20 Jun 2006
Posts: 12

PostPosted: Fri Jun 30, 2006 8:08 am    Post subject: Re: Variable Number of Arguments in Macro Reply with quote

On 29 Jun 2006 07:05:00 -0700, Praveen.Kumar.SP@gmail.com wrote:
Quote:
Hi
Could anyone solve the problem for the code below

The Code:

#include "stdio.h"
#include "iostream.h"

void Temp( int a, char* str,...)
{
//code to handle the arguments
}

#define MYPRINT(_x_) printf _x_
#define MYPRINT1(_x_) Temp( 10,_x_)

int main()
{
MYPRINT(("This is a test for multiple argument %s
%d",__FILE__,__LINE__));
MYPRINT1(("This is a test for multiple argument %s
%d",__FILE__,__LINE__));

return 0;
}

it seems easy in assembly
xxxx:
xxx xx x, x

_MYPRINT1:
xxx xxxxxxxxxxx
xxxx xx
xxxx _Temp
xxxx xxxxxxxxxxx
xxx

to call in this way
MYPRINT1("This is a test for multiple argument %s%d",
__FILE__,__LINE__);
but it is OT and so i have write 'x' where there are alphabeticals
letters (don't counting _Temp)

but hey, here *you* are all the smart guys so find some other solution
in C++ Smile
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [11 Posts] View previous topic :: View next topic
The time now is Tue Dec 02, 2008 2:49 pm | All times are GMT
navigation Forum index » Programming » C++
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts Postfix + MySQL error: very strange variable %s iWarior Postfix 0 Mon Aug 25, 2008 2:01 pm
No new posts User Environment - export PATH variable paalepu AIX 0 Tue Sep 12, 2006 8:12 pm
No new posts number of words in a line Fred J. C++ 3 Fri Jul 21, 2006 3:52 am
No new posts output number mm.omid@gmail.com C++ 1 Thu Jul 20, 2006 5:09 pm
No new posts to_char number format with optional decimal-point? Martin T. Oracle 3 Thu Jul 20, 2006 10:53 am

Credit Cards | Mortgages | Loans | Charity | Web Advertising
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.2261s ][ Queries: 16 (0.0999s) ][ GZIP on - Debug on ]