Solved

Variable arguments for a function

Posted on 2003-11-04
9
319 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
9 Comments
 
LVL 45

Accepted Solution

by:
sunnycoder earned 25 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 25 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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 25 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

708 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now