473,382 Members | 1,359 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Determining Number of Arguments

The number of arguments passed to the following functions can be
determined - but how is this coded?

main (int argc, char *argv[])
printf ( string format [, mixed args])

I would like to be able to write arbitrary functions that know how many
arguments were passed to them.
Jul 22 '05 #1
3 1720
Trevor M. Lango wrote:
The number of arguments passed to the following functions can be
determined - but how is this coded?

main (int argc, char *argv[])
main is a special function. You're not allowed to call it.
printf ( string format [, mixed args])

I would like to be able to write arbitrary functions that know how
many arguments were passed to them.


This is a function with a variable argument list. It's declared
something like:

int printf(const char* format, ...);

The ellipsis (...) indicates a variable list of arguments. The actual
number of arguments is determined from the format string. You have to
provide your own way for the function to find out the number and type
of arguments if you want to use variable argument lists. This is one
reason why you actually shouldn't do that. It's not type-safe and can
easiliy lead to errors that the compiler cannot detect. Another
disadvantage is that you can only pass POD types. Alternatively, you
can write a function or operator that 'writes' to an object and returns
a reference to that object, so that calls to that funciton/operator can
be stacked together. This is e.g. done in the C++ stream classes and
their operator<< and >>.

Jul 22 '05 #2
On Thu, 19 Feb 2004 08:21:23 GMT, "Trevor M. Lango"
<tm*****@sbcglobal.net> wrote in comp.lang.c++:
The number of arguments passed to the following functions can be
determined - but how is this coded?

main (int argc, char *argv[])
printf ( string format [, mixed args])

I would like to be able to write arbitrary functions that know how many
arguments were passed to them.


The only response I see so far just tells you that "you shouldn't do
it". But it is perfectly legal in C and C++, and the languages
provide standard features to do so.

See http://www.eskimo.com/~scs/C-faq/s15.html in the FAQ for
comp.lang.c for a starter on using <stdarg.h> or <cstdarg>.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 22 '05 #3
Jack Klein wrote:
On Thu, 19 Feb 2004 08:21:23 GMT, "Trevor M. Lango"
<tm*****@sbcglobal.net> wrote in comp.lang.c++:
The number of arguments passed to the following functions can be
determined - but how is this coded?

main (int argc, char *argv[])
printf ( string format [, mixed args])

I would like to be able to write arbitrary functions that know how
many arguments were passed to them.
The only response I see so far just tells you that "you shouldn't do
it".


No, I also gave some of the reasons why "you shouldn't do it". I also
told how to declare a function with variable argument lists. I just
forgot to mention the <cstdarg> header. The rest should be available in
the OP's compiler documentation.
But it is perfectly legal in C and C++, and the languages
provide standard features to do so.


I never claimed anything else.

Jul 22 '05 #4

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

Similar topics

66
by: Darren Dale | last post by:
Hello, def test(data): i = ? This is the line I have trouble with if i==1: return data else: return data a,b,c,d = test()
8
by: Mark English | last post by:
I'd like to write a Tkinter app which, given a class, pops up a window(s) with fields for each "attribute" of that class. The user could enter values for the attributes and on closing the window...
2
by: William Payne | last post by:
Hello, I am making a very simple and crude Makefile generator, that currently supports three different options: --project-name=<name_of_project>...
7
by: Jean-David Beyer | last post by:
I have six hard drives (4 SCSI and 2 EIDE) on my main machine with parts of a database on each drive. The main index is on one SCSI drive all to itself. The main data are on the other three SCSI...
5
by: Lyn | last post by:
Is there any way of determining in VBA the name and arguments of the currently executed procedure? To assist in debugging, I would like to be able to trace the procedures as they are executed. ...
9
by: Schraalhans Keukenmeester | last post by:
I have some C functions (with variable length argument lists) that use void pointers as arguments. Is there a way to determine at runtime what type of parameter is actually passed on to the...
3
by: dune1 | last post by:
I am having so much trouble with this:( How would I write a script that compares the integer values in two 5 element arrays. My script should work like this: -user enters 5 integers into the...
2
by: Alex | last post by:
Hello, I'm writing a console app in VB 2005, and it needs to accept 3 arguments from the command line. I've already added an If statement which checks to see if arg(0) is "/?" or "-?" to return...
53
by: Aaron Gray | last post by:
Due to M$'s stupidity in not making DOMElements first class citizens the following will not work :- function isElement( o) { return o instanceof Element } It works for FF, Opera and Safari.
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.