Solved

Variable arguments for a function

Posted on 2003-11-04
9
348 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
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.

828 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