?
Solved

Variable arguments for a function

Posted on 2003-11-04
9
Medium Priority
?
365 Views
Last Modified: 2010-04-15
I want to know i can i define a function which accepts any no of arguments.
In MSDN there is an example given but in that we have to specify the sequence in which the data type will appear or there is another example which accepts any no. of integer data but the argument list always ends with -1.

I want something like this

int variable(int i,...);
and this function should a accept any no. of arguments in any order.
The data type can be user defined or all other basic types.

is it possible?

thanks n regards
himanshu
0
Comment
Question by:mittal_himanshu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 45

Accepted Solution

by:
sunnycoder earned 100 total points
ID: 9676987
Hi himanshu,

the type of declaration you want is perfectly valid and possible as an example try compiling and executing this program

#include <stdio.h>
void p(int i,...);

int main ()
{
        p (5,6,7);
}

void p(int i,...)
{
        printf("%d\n",i);
}

to get the list of arguments passed, use getopts()              
0
 
LVL 10

Assisted Solution

by:Sys_Prog
Sys_Prog earned 100 total points
ID: 9677004
The same thing, the function prototype is OK

One more way to access the optional arguments

Look at the help for following macros/Data Type

va_start
va_arg
va_end
va_list


0
 

Author Comment

by:mittal_himanshu
ID: 9677619
i have tried va_start, va_arg..etc...macro.
void main()
{
    ShowVar( "fcsi", 32.4f, 'a', "Test string", 4 );
}
void ShowVar( char *szTypes, ... )
{
    va_list vl;
    int i;
    va_start( vl, szTypes );

        for( i = 0; szTypes[i] != '\0'; ++i )
    {
        union Printable_t
        {
            int     i;
            float   f;
            char    c;
            char   *s;
        } Printable;

        switch( szTypes[i] )    // Type to expect.
        {
        case 'i':
            Printable.i = va_arg( vl, int );
            printf( "%i\n", Printable.i );
            break;

        case 'f':
            Printable.f = va_arg( vl, float );
            printf( "%f\n", Printable.f );
            break;

        case 'c':
            Printable.c = va_arg( vl, char );
            printf( "%c\n", Printable.c );
            break;

        case 's':
            Printable.s = va_arg( vl, char * );
            printf( "%s\n", Printable.s );
            break;

        default:
            break;
        }
    }

In the above program we have to give details of all the types expected and the sequence.

i want a function which say accepts any no. of arguments in any sequence.


how can i use getops()?
there is a function GetOption() but its for command line arguments.

you have given the code to print only 1st argument. how can i print the second ,third and the remaining parameters of different types?





 
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Expert Comment

by:Sys_Prog
ID: 9677642
I think in that case, u should read everything as char * and then may be convert to appropriate data if required (as per your convenience)

Becuase in case of cmd line options also, u get them as char * and then u need to decipher them as per your logic
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9677661
getopt() is used for command line args only ... for args in other functions, you have to depend on va_*

alternatively you can use function pointers (if they suit your scenario) as shown here
http://oldlook.experts-exchange.com/Programming/Programming_Languages/C/Q_20785921.html
0
 
LVL 17

Assisted Solution

by:rstaveley
rstaveley earned 100 total points
ID: 9677920
You're pretty much there, mittal_himanshu, you were just bitten by the fact that floats are promoted to doubles and chars are promoted to ints.

This works:
--------8<--------
#include <stdio.h>
#include <stdarg.h>

void ShowVar(char*,...);

int main()
{
    ShowVar( "fcsi", 32.4f, 'a', "Test string", 4 );
}
void ShowVar( char *szTypes, ... )
{
    va_list vl;
    int i;
    va_start( vl, szTypes );

        for( i = 0; szTypes[i] != '\0'; ++i )
    {
        union Printable_t
        {
            int     i;
            float   f;
            char    c;
            char   *s;
        } Printable;

        switch( szTypes[i] )    // Type to expect.
        {
        case 'i':
            Printable.i = va_arg( vl, int );
            printf( "%i\n", Printable.i );
            break;

        case 'f':
            Printable.f = va_arg( vl, /*float*/ double ); /* Fixed for float -> double promotion */
            printf( "%f\n", Printable.f );
            break;

        case 'c':
            Printable.c = va_arg( vl, /*char*/ int ); /* Fixed for char -> int promotion */
            printf( "%c\n", Printable.c );
            break;

        case 's':
            Printable.s = va_arg( vl, char * );
            printf( "%s\n", Printable.s );
            break;

        default:
            break;
        }
    }
}
--------8<--------

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question