473,804 Members | 3,909 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

stdarg problems

Hi,

Can someone tell me what is wrong with the below program?
-------------------------------------------
int main()
{
f1( 25 50,"testing stdarg. ", "it is working", "if it is
working." );
return 0;
}

void f1(bool condition, const char * msg,...)
{
va_list ap;
va_start(ap, msg);

while (msg != 0 )
{
cout<< msg<<endl;
msg = va_arg(ap, const char *);
}
va_end(ap);

}
--------------------------------------------

the ouput is
------------------------------------------
testing stdarg.
it is working
if it is working.

UH??AWAVAUATSH? ?8H?E?
------------------------------------------

Thanks,
NarikNahom

Jul 1 '07
13 2145
na********@gmai l.com wrote:
Is the ellipsis( ... ) a part of C/C++
Yes. When C++ was designed, C generally used the syntax

int foo();

to declare functions. C had no type checking of the arguments, so this
means «declare function foo to take any number of arguments, and return an
int». In really old text books, code would often just declare functions
from the standard library instead of including the proper header. Foo could
be defined in another compilation unit as:

int foo(bar)
int bar;
{ return do_stuff(bar); }

As you can see, «any number of arguments» usually means «whatever the
function expects». Dennis Ritchie has commented on this here:
http://www.lysator.liu.se/c/chistory.ps.

For another comment on the implications, I refer to the third commandment:
http://www.lysator.liu.se/c/ten-commandments.html

In C++, the function declaration syntax came to use the syntax we know
today, but to support the concept «any number of arguments», as some C
library functions use, ... was introduced.
or stdarg.h?
No.
If its part of the C/C++, then shouldnt the compiler know where the end of
the arguments is
Yes.
and automatically insert a NULL?
No.
Maybe this is not how C/C++ is designed but what are the disadvantages if
it is so?
First of all, what is NULL? In C++, it's an integral constant, while in C,
it's an implemention defined pointer constant. There's no general way for
the compiler to know what types a function expects for ..., and it would be
nice if you could use more than one type in ...? So, while there are no
general disadvantages, there are no real advantages either. And if you can,
you should avoid using ... in functions.

--
rbh
Jul 2 '07 #11
na********@gmai l.com wrote:
Is the ellipsis( ... ) a part of C/C++ or stdarg.h?
The ellipsis is part of both C and C++. stdarg.h is too. It provides the
facilities for using variable argument lists.
If its part of the C/C++, then shouldnt the compiler know where the end of
the arguments is and automatically insert a NULL?
What if I want some other way to determine the end, e.g. if NULL pointers
can be anywhere within the argument list? What if I want to pass integers
as arguments and not pointers? Which value should be inserted for the final
argument? How would the compiler know if my function expects an integer or
a pointer?
Jul 2 '07 #12
On Jul 2, 2:26 pm, "narikna...@gma il.com" <narikna...@gma il.com>
wrote:
Is the ellipsis( ... ) a part of C/C++ or stdarg.h? If its part of the
C/C++, then shouldnt the compiler know where the end of the arguments
is and automatically insert a NULL?
Why? How? The C++ compiler has no idea as to what conventions
are being used to determine the number and type of the
arguments. As Ron pointed out, support for variable args was
really first designed to handle printf and company. No "NULL"
is needed, and since the argument types can vary, what type
should it be if the compiler generated it.

The ellipsis is present in C++ for historical reasons. When
dealing with a legacy interface, like printf or execl, conform
to what that interface requires. And don't use it in new code;
there are always better solutions.
Maybe this is not how C/C++ is
designed but what are the disadvantages if it is so?
Except for the fact that its unimplementable and doesn't make
sense, nothing.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jul 3 '07 #13
On Jul 2, 4:47 pm, Robert Bauck Hamar <roberth+n...@i fi.uio.nowrote:
First of all, what is NULL? In C++, it's an integral constant,
while in C, it's an implemention defined pointer constant.
In both languages, it's a "null pointer constant". In both
languages, the "usual" (and traditional) definition is simply
"0". In both languages, however, any integral constant
expression will do the trick. C also allows such expressions
cast to void*, but except under MS-DOS, it's rather exceptional.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jul 3 '07 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
3455
by: Klaus Schneider | last post by:
Hi all! I'm having trouble with a template function with variable arguments when I parse an enum type as variable argument. Example: template <class T> bool test(int num, ...) { va_list ap; int ind;
5
8802
by: Corky | last post by:
This works: db2 SELECT DISTINCT PROBLEM_OBJECTS.PROBLEM_ID FROM PROBLEM_OBJECTS INNER JOIN PROBLEMS ON PROBLEM_OBJECTS.PROBLEM_ID = PROBLEMS.PROBLEM_ID WHERE INTEGER(DAYS(CURRENT DATE) - DAYS(PROBLEMS.CLOSE_DATE)) = 365 AND PROBLEMS.CLOSE_DATE IS NOT NULL But this doesn't: db2 SELECT DISTINCT PROBLEM_OBJECTS.PROBLEM_ID FROM PROBLEM_OBJECTS
5
3037
by: Francesco Bochicchio | last post by:
Hi all, anybody knows if there is a (standard, portable) way to dinamically build a list of parameters to call a C function? Something like va_start & co, but to be used on the calling side? In other words, suppose that I have a generic pointer : void * f_ptr; I know that this pointer points to a fuction. I also know the function
6
3851
by: Clint Olsen | last post by:
I had this crazy idea to use stdarg macros to copy data in a generic fashion, regardless of type. However, I'm not sure it will work in part due to the ANSI C default argument promotions and the fact that va_arg requires a type to decide how far to advance to subsequent arguments. So, essentially what I want in the variable argument function is something that just assigns to a character type, and then copy a predefined number of bytes to...
9
1829
by: Mac A. Cody | last post by:
Hello, I'm encountering a problem with using stdarg that I cannot figure out. I'm trying to develop a function for a linux driver module that takes a variable-length sequence of u8-type values. Below is the function: #include <stdarg.h> .. ..
10
2417
by: BBFrost | last post by:
We just recently moved one of our major c# apps from VS Net 2002 to VS Net 2003. At first things were looking ok, now problems are starting to appear. So far ... (1) ComboBox.SelectedValue = db_value; If the db_value was not included in the ComboBox value list the ComboBox.SelectedIndex used to return -1, Now the very same code is
3
5129
by: cman | last post by:
Can somebody explain the following code segment, from stdargs.h (from linux 0.01) #define __va_rounded_size(TYPE) \ (((sizeof (TYPE) + sizeof (int) - 1) / sizeof (int)) * sizeof (int))va_rounded_size #define va_start(AP, LASTARG) \ (__builtin_saveregs (), \ AP = ((char *) &(LASTARG) + __va_rounded_size (LASTARG)))
9
5468
by: tonybalinski | last post by:
I'd like to be able to scan a va_list twice in a v... function, but can't see how to do it. For example char *strdupcat(const char *first, ...) { char *result, pos; va_list arg; size_t len; const char *next;
0
10575
Oralloy
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...
1
10319
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10076
tracyyun
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...
1
7616
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6851
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();...
0
5520
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...
1
4297
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3816
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2990
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.