• C

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
mittal_himanshuAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
Simple Misconfiguration =Network Vulnerability

In this technical webinar, AlgoSec will present several examples of common misconfigurations; including a basic device change, business application connectivity changes, and data center migrations. Learn best practices to protect your business from attack.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.