variable argument list

Go down

variable argument list

Post  Pulkit on Tue Aug 12, 2008 11:01 am

Write a function which accepts variable number of arguments and print all of them ..

If u done with this write a macro for the same...

Pulkit

Posts : 2
Join date : 2008-08-10

View user profile

Back to top Go down

Re: variable argument list

Post  Dee2306 on Tue Aug 12, 2008 11:27 am

Pulkit wrote:Write a function which accepts variable number of arguments and print all of them ..

If u done with this write a macro for the same...

Hiii Pullu...cyclops
int main(int argc, char argv[])
{
for(int i=1; i<argc; i++) printf("%s\n",argv[i]);
}

//Does It Suffice.. ?? Or You Are asking 4 something else.. Shocked
avatar
Dee2306

Posts : 7
Join date : 2008-08-12
Age : 30

View user profile

Back to top Go down

use CSTDARG

Post  prani on Tue Aug 12, 2008 12:43 pm

the header file cstdarg provides for such situation,,
here is sample code............
#include <cstdarg>
#include <iostream>

using namespace std;

double average ( int num, ... )
{
va_list arguments; // A place to store the list of arguments
double sum = 0;

va_start ( arguments, num ); // Initializing arguments to store all values after num
for ( int x = 0; x < num; x++ ) // Loop until all numbers are added
sum += va_arg ( arguments, double ); // Adds the next value in argument list to sum.
va_end ( arguments ); // Cleans up the list

return sum / num; // Returns some number (typecast prevents truncation)
}
int main()
{
cout<< average ( 3, 12.2, 22.3, 4.5 ) <<endl;
cout<< average ( 5, 3.3, 2.2, 1.1, 5.5, 3.3 ) <<endl;
}
more@http://www.cprogramming.com/tutorial/lesson17.html

prani

Posts : 15
Join date : 2008-08-12

View user profile

Back to top Go down

Re: variable argument list

Post  Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum