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 10 9848 Pr************* *@gmail.com wrote: 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.
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
* Pr************* *@gmail.com: Could anyone solve the problem for the code below
"The" problem? I count a multitude of problems. Which one?
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.
#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.
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.
{ //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.
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.
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?
Alf P. Steinbach wrote: * Pr************* *@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?
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
* Victor Bazarov: Alf P. Steinbach wrote: * Pr************* *@gmail.com: [..] The second macro invocation does not work because it expands to
Temp( 10, ("some text",__FILE__, __LINE__));
Really?
Yep.
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.
Why?
Because that's how macros work; look it up in your favorite C++ textbook.
Have you tried it?
No. which is syntactically invalid.
Why is it invalid?
There you caught me. ;-) 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?
Alf P. Steinbach wrote: * Victor Bazarov: Alf P. Steinbach wrote: * Pr************* *@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. which is syntactically invalid.
Why is it invalid?
There you caught me. ;-) 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
* Victor Bazarov: Alf P. Steinbach wrote: * Victor Bazarov: Alf P. Steinbach wrote: * Pr************* *@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. 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? Pr************* *@gmail.com wrote: 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).
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.
--
Frederick Gotham
Frederick Gotham wrote: 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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Martin Magnusson |
last post by:
I'm using a matrix and vector library, that won't compile. When
running g++ I get the error message "macro "minor" passed 5 arguments,
but takes just 1"
The definition of "minor" looks like below, and it takes 3 arguments.
All calls to minor that I have found in the code also pass it three
arguments, so I really don't understand this error.
Does anything look suspicious with the following definition, or must
it be that there is some...
|
by: Walter L. Preuninger II |
last post by:
I would like to write a generic procedure that will take string or numeric
variables. I can not think of a way to make this more clear except to show
what I want.
int main(void)
{
int i=7;
char *s="/etc/filesystems";
generic(i);
|
by: Augustus S.F.X Van Dusen |
last post by:
I have recently come across the following construction:
#define P_VAR(output, string, args...) \
fprintf (output, "This is "string"\n", ##args)
which can be invoked as follows:
int x = 1, y = 2 ;
char * str = "String" ;
|
by: Michael McGarry |
last post by:
Hi,
I am pretty sure this is not possible, but maybe somehow it is.
Given a variable, can I tell what type it is at runtime?
Michael
|
by: Nimmi Srivastav |
last post by:
Consider two functions A and B, both of which accept a variable number
of arguments (va_start, va-arg, va_end). Is there an easy way for
arguments passed to A to be, in turn, passed to B? (For example, if A
is a wrapper function around B).
Thanks,
Nimmi
| |
by: carvalho.miguel |
last post by:
hello,
imagine you have a static class method that receives a function
pointer, an int with the number of arguments and a variable number of
arguments.
in that static method you want to call that function (using its
pointer) and call it with the same argument list that was passed into
the static method.
|
by: Max TenEyck Woodbury |
last post by:
I have a static array that requires quite complicated initialization.
With C89 I constructed a set of macros that greatly simplified that
process but required the user to count the number of arguments. With
the advent of C99, I can get around that problem. However I wish to
hide even more of the implementation details being yet another macro.
The macro would have a form something like SUPER_TABLE( name, (...)...)
where the (...)...
|
by: rashmi |
last post by:
Hello All,
Can we map a MACRO with variable number of arguments to a function with
variable number of arguments?
Please help me in finding out how this could be done ?
for eg:
#define MY_MACRO(int mid,int mlevel,...)
my_func(mid,mlevel,format,##args)
void my_func(int mid,int mlevel,char *format,....)
{
va_list ap;
|
by: CptDondo |
last post by:
How do you declare a function with 0 or mroe arguments?
I have a bunch of functions like this:
void tc_cm(int row, int col);
void tc_do(void);
void tc_DO(int ln);
and I am trying to declare a pointer to them:
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |