Stdarg: Variable Arguments
Related Blog Items
- Name Mangling
- Calling conventions on the x86 platform
- C++ Advanced Tutorial - Lesson 7
- Advanced Test in C: The 0x0A Best Questions for C Programmers
- Frame Pointer Vs Stack Pointer
ANSI C provides macros to support for accessing variable arguments of a function. These macros are va_start, va_arg, va_end.
Which include file to use:
stdarg.h
APIs:
void va_start(va_list ap, last);
type va_arg(va_list ap, type);
void va_end(va_list ap);
void va_copy(va_list dest, va_list src);
Here is the man page of stdarg
How to declare a variable arguments function:
int varargsfunction(int numargs, …);
“…” three dots at the end signifies variable arguments.
How to call variable argument functions:
here are some examples..
varargsfunction(3, “aa”, “bb”, “cc”);
varargsfunction(”%s%d%s”, “aa”, 22, “cc”);
varargsfunction(”aa”, “bb”, “cc”, NULL);
How to recognize number of arguments, variable types in function while accessing them:
For accessing and processing variables we require basically
How many arguments passed
Types of arguments
Here are the methods to identify number of arguments and type of arguments
1)if you know the variable types you are passing then mentioning number of arguments while calling variable argument function would be sufficient. Consider an example where you are passing a string then all integers
4ff2466d3b04ca34348b4d495d4cbfbc000
You can call this function as something like
stdargs_function(4, “ccc”, 23,43,45);
2)if you know the types of variables you are passing then to identify number of arguments passing, terminate the argument list with NULL.
int stdargs_function(int msg, ...) { char *msg; va_list ap; va_start(ap, numargs); for (; msg != NULL;) { msg = va_arg(ap, char *); } va_end(ap); return 0; }
You can call this function as something like
stdargs_function(”ccc”, “dd”, NULL);
3)make available number of arguments and type arguments available in first argument (ex. printf function)
int stdargs_function(int format_string, ...) { char *msg; va_list ap; numargs = GetNumArgs(format_string); va_start(ap, format_string); for (i=0;i<numargs; i++) { type = GetNextFormatSpecifier(format_string); switch(type) { case 'd': msg = va_arg(ap, char *); break; case 's': msg = va_arg(ap, int); break; } } va_end(ap); }
You can call this function as something like
stdargs_function(”%d%s”, 23, “dd”);
Is there any other implementation other than stdarg.h:
Yes, varargs.h was used before ANSI C. varargs.h is obsolete now. Varargs usage is slightly different
Than stdargs usage. Stdargs require at least one argument need to be mentioned before using variable args, varargs doesn’t mandate this.
An Example Program:
#include <stdio.h> #include <stdarg.h> int print(int max, ...); int main(void) { print(2, "OpenAsthra", "formula pattern"); print(1, "we are done with this program"); return 0; } int print(int numargs, ...) { va_list ap; int args = 0; char *strings[10]; va_start(ap, numargs); while(args < numargs) { strings[args] = va_arg(arg_ptr, char *); printf("%s\n";, strings[args++]); } va_end(ap); }
Popularity: 10%
You need to log on to convert this article into PDF
Related Blog Items - Name Mangling
- Calling conventions on the x86 platform
- C++ Advanced Tutorial - Lesson 7
- Advanced Test in C: The 0x0A Best Questions for C Programmers
- Frame Pointer Vs Stack Pointer
Related Blog Items
- Name Mangling
- Calling conventions on the x86 platform
- C++ Advanced Tutorial - Lesson 7
- Advanced Test in C: The 0x0A Best Questions for C Programmers
- Frame Pointer Vs Stack Pointer
cool..
February 15th, 2007 at 9:04 pm