473,406 Members | 2,356 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,406 software developers and data experts.

variable number of arguments

I read the C FAQ question on passing a variable number of arguments,
but it didn't help. The example assumes all arguments are of the same
type.

I want to create a function "trace" that can be used like this:

trace( "Err", errtType, lineNum, NULL) /* where errType is char* and
lineNum is
an int */

trace("Err", lineNum, NULL);

trace( lineNum, "Err", funcName, NULL) /* where funName is char * */

The problem is that I don't know the order and the variable types of
the arguments passed.

How can I implement trace?
Nov 14 '05 #1
10 4993
th***********@hotmail.com (The Directive) writes:
I want to create a function "trace" that can be used like this:

trace( "Err", errtType, lineNum, NULL) /* where errType is char* and
lineNum is
an int */

trace("Err", lineNum, NULL);

trace( lineNum, "Err", funcName, NULL) /* where funName is char * */

The problem is that I don't know the order and the variable types of
the arguments passed.

How can I implement trace?


You can't. trace() has to know the types of the arguments, or be
able to figure them out.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
Nov 14 '05 #2
Hi
I already wrote a function of the same type. But I believe that it is
necessary to have at least a parameter which will describe the following.
Like does it printf, you could have: trace(char format,...);
with for example:
trace("Err Line:%d Type:%c File name:%s,__LINE__,mytype,__FILE__);
If you want, I can send my source
Dominique

"The Directive" <th***********@hotmail.com> a écrit dans le message de
news:84**************************@posting.google.c om...
I read the C FAQ question on passing a variable number of arguments,
but it didn't help. The example assumes all arguments are of the same
type.

I want to create a function "trace" that can be used like this:

trace( "Err", errtType, lineNum, NULL) /* where errType is char* and
lineNum is
an int */

trace("Err", lineNum, NULL);

trace( lineNum, "Err", funcName, NULL) /* where funName is char * */

The problem is that I don't know the order and the variable types of
the arguments passed.

How can I implement trace?

Nov 14 '05 #3
Oups error:
trace(char *format, ...);

"DomiPi" <ak******@tiscali.be> a écrit dans le message de
news:bt**********@news.worldonline.be...
Hi
I already wrote a function of the same type. But I believe that it is
necessary to have at least a parameter which will describe the following.
Like does it printf, you could have: trace(char format,...);
with for example:
trace("Err Line:%d Type:%c File name:%s,__LINE__,mytype,__FILE__);
If you want, I can send my source
Dominique

"The Directive" <th***********@hotmail.com> a écrit dans le message de
news:84**************************@posting.google.c om...
I read the C FAQ question on passing a variable number of arguments,
but it didn't help. The example assumes all arguments are of the same
type.

I want to create a function "trace" that can be used like this:

trace( "Err", errtType, lineNum, NULL) /* where errType is char* and
lineNum is
an int */

trace("Err", lineNum, NULL);

trace( lineNum, "Err", funcName, NULL) /* where funName is char * */

The problem is that I don't know the order and the variable types of
the arguments passed.

How can I implement trace?


Nov 14 '05 #4
"DomiPi" <ak******@tiscali.be> wrote in message
news:bt**********@news.worldonline.be...
Oups error:
trace(char *format, ...);


Oups (sic) error: top-post.

<rest snipped for clarity>

Peter
Nov 14 '05 #5
Peter Pichler wrote:
"DomiPi" <ak******@tiscali.be> wrote in message
Oups error:
trace(char *format, ...);


Oups (sic) error: top-post.

<rest snipped for clarity>


A fine riposte.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #6
"DomiPi" <ak******@tiscali.be> writes:
I already wrote a function of the same type. But I believe that it is
necessary to have at least a parameter which will describe the following.
Like does it printf, you could have: trace(char format,...);
with for example:
trace("Err Line:%d Type:%c File name:%s,__LINE__,mytype,__FILE__);
If you want, I can send my source


There are a number of ways that a function taking a variable number of
arguments can be told the number and type of the arguments. printf's
format string is probably the most common. If all the arguments
(after the initial one(s)) are of the same type, a sentinel value,
such as a null pointer, can be used to mark the end of the list.

Another approach (one that I've never actually seen) is for every
other argument to specify the type of the following one:

foo('i', 42,
'd', 123.4,
's', "hello");

Whatever method is used, it's the caller's responsibility to make sure
everything is consistent. If you pass a pointer where the function
looks for an int, you're not likely to get any help from the compiler.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 14 '05 #7
From everyone's responses, I conclude that there's no easy, clean way
to pass a variable number of arguments with variable unknown types in
C. Correct? That information must be passed as another argument. What
was the reasoning for the C language designers to implement it this
way? Is it a weakness of the language?

--The Directive

Keith Thompson <ks***@mib.org> wrote in message news:<ln************@nuthaus.mib.org>...
"DomiPi" <ak******@tiscali.be> writes:
I already wrote a function of the same type. But I believe that it is
necessary to have at least a parameter which will describe the following.
Like does it printf, you could have: trace(char format,...);
with for example:
trace("Err Line:%d Type:%c File name:%s,__LINE__,mytype,__FILE__);
If you want, I can send my source


There are a number of ways that a function taking a variable number of
arguments can be told the number and type of the arguments. printf's
format string is probably the most common. If all the arguments
(after the initial one(s)) are of the same type, a sentinel value,
such as a null pointer, can be used to mark the end of the list.

Another approach (one that I've never actually seen) is for every
other argument to specify the type of the following one:

foo('i', 42,
'd', 123.4,
's', "hello");

Whatever method is used, it's the caller's responsibility to make sure
everything is consistent. If you pass a pointer where the function
looks for an int, you're not likely to get any help from the compiler.

Nov 14 '05 #8

On Mon, 11 Jan 2004, The Directive wrote:

From everyone's responses, I conclude that there's no easy, clean way
to pass a variable number of arguments with variable unknown types in
C. Correct? That information must be passed as another argument. What
was the reasoning for the C language designers to implement it this
way? Is it a weakness of the language?


Please snip the part of the previous message to which you're not
responding. And don't top-post. http://www.google.com/search?q=top+post

Now, as to your question: Correct. That information (about types)
must be passed as another argument, or built into the structure of the
arguments in the first place. (For example, you could pass a list of
positive integers to a function, and end the list with -1. The
function would know to stop reading arguments when it hit that -1.)
The C language was designed this way because it's incredibly easy
to code, and it's pretty fast [with relatively naive optimization], too,
compared to other ways. Compare the C model to the "Pascal" model,
which can't support variadic functions at all. Search Google for
"argument passing conventions," and then try comp.compilers,
comp.programming, or a similar group if you have implementation
questions.
It's a weakness of the language when compared to languages that
provide more functionality (such as a count of arguments magically
supplied by the compiler); but off the top of my head, the only
language I know that does better is Matlab, and it's got its own
weaknesses. :) It's a trade-off between ease of implementation, power,
and user-friendliness. Since most users don't define functions where
they don't know what they're going to be passing as arguments, it
probably didn't seem a big deal when C was being standardized.

Historical note: I seem to recall that in pre-1989 days, there
was no [de facto] language support for variadic functions at all.
printf and scanf worked by magic; the user was not generally able to
write his own printf-alikes. Do I recall correctly?

-Arthur

Nov 14 '05 #9
"Arthur J. O'Dwyer" <aj*@nospam.andrew.cmu.edu> writes:
[...]
Historical note: I seem to recall that in pre-1989 days, there
was no [de facto] language support for variadic functions at all.
printf and scanf worked by magic; the user was not generally able to
write his own printf-alikes. Do I recall correctly?

-Arthur


There was a <varargs.h> header that predated the current <stdarg.h>
header. Since <stdarg.h> is in the 1989 ANSI standard, <varargs.h>
must have existed before 1989.

But I think that there was a time, before <varargs.h> was invented,
when printf and scanf worked by magic (e.g., by explicitly computing
stack offsets for successive arguments).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 14 '05 #10
Keith Thompson <ks***@mib.org> writes:
There was a <varargs.h> header that predated the current <stdarg.h>
header. Since <stdarg.h> is in the 1989 ANSI standard, <varargs.h>
must have existed before 1989.

But I think that there was a time, before <varargs.h> was invented,
when printf and scanf worked by magic (e.g., by explicitly computing
stack offsets for successive arguments).


Here's the printf() from the Lions Book circa 1976. All typos
are mine.

/*
* Scaled down version of C Library printf.
* Only %s %l %d (==%l) %o are recognized.
* Used to print diagnostic information
* directly on console tty.
* Since it is not interrupt driven,
* all system activities are pretty much
* suspended.
* Printf should not be used for chit-chat.
*/
printf(fmt,x1,x2,x3,x4,x5,x6,x7,x8,x9,xa,xb,xc)
char fmt[];
{
register char *s;
register *adx, c;

adx = &x1;
loop:
while((c = *fmt++) != '%') {
if(c == '\0')
return;
putchar(c);
}
c = *fmt++;
if(c == 'd' || c == 'l' || c == 'o')
printn(*adx, c=='o'? 8: 10);
if(c == 's') {
s = *adx;
while(c == *s++)
puthcar(c);
}
adx++;
goto loop;
}
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
Nov 14 '05 #11

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

Similar topics

2
by: Suzanne Vogel | last post by:
'stdarg.h' defines the 'va_arg' type to use in passing variable numbers of parameters to functions. The example of its use given in the Dinkumware documentation *seems* to imply that the 'va_arg'...
7
by: Kapt. Boogschutter | last post by:
I'm trying to create a function that has at least 1 Argument but can also contain any number of Arguments (except 0 because my function would have no meaning for 0 argument). The arguments...
134
by: James A. Donald | last post by:
I am contemplating getting into Python, which is used by engineers I admire - google and Bram Cohen, but was horrified to read "no variable or argument declarations are necessary." Surely that...
5
by: Sathyaish | last post by:
I knew this but I have forgotten. How do you declare a function that has to accept a variable number of arguments? For instance, the printf() function has a variable number of arguments it can...
23
by: Russ Chinoy | last post by:
Hi, This may be a totally newbie question, but I'm stumped. If I have a function such as: function DoSomething(strVarName) { ..... }
3
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...
16
by: utab | last post by:
Dear all, How can I generate a constructor to take a variable number of arguments. Is boost tuple appropriate for this? I know that there is a way to make functions take variable number of...
2
by: Ramashish Baranwal | last post by:
Hi, I need to process few out of a variable number of named arguments in a function and pass the remaining to another function that also takes variable number of named arguments. Consider this...
2
by: Alan | last post by:
I have a couple of questions about using a variable number of arguments in a function call (...). The context is that I have some mathematical functions I created. I currently pass them a pair of...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
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,...
0
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...
0
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,...
0
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...
0
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...
0
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...

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.