• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 376
  • Last Modified:

Variable arguments for a function

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
mittal_himanshu
Asked:
mittal_himanshu
3 Solutions
 
sunnycoderCommented:
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
 
Sys_ProgCommented:
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
 
mittal_himanshuAuthor Commented:
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
Industry Leaders: 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!

 
Sys_ProgCommented:
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
 
sunnycoderCommented:
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
 
rstaveleyCommented:
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now