473,545 Members | 1,998 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1733
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*****@sbcglo bal.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.l earn.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*****@sbcglo bal.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
4909
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
2521
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 would be returned an instance of the class. The actual application I'm interested in writing would either have simple type attributes (int, string,...
2
1272
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> --source-files=<source_file_names_separated_by_commas> --resource-file=<name_of_resource_file_if_any> The first thing I do is to put all program arguments (excluding argv) in a std::vector of...
7
2117
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 drives. Small relations are on one EIDE drive, and the logfiles are on the other EIDE drive. When running the task, below, the rest of the machine is...
5
2985
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. Then, when it crashes with Access's usual unhelpful error messages, at least I will be able to determine in which procedure it happened. What I...
9
2778
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 function? PHP and my oldskool turbopascal provide a typeof() function, but my C compiler (gcc 3.4.1) does not seem to provide this function. Perhaps...
3
8065
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 first array -user enters 5 integers into the second array -script finds integers that the two arrays have in common and stores them in a third...
2
977
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 some help, but how do I also show help if there are no arguments? I tried doing arg(0) = "" and also IsNothing(args(0)) which doesn't seem to work...
53
4903
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.
0
7473
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7406
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7660
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. ...
0
7813
jinu1996
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...
1
7431
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...
0
7761
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...
0
5976
agi2029
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...
0
4949
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...
1
1020
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.