variable argument list

Post new topic   Reply to topic

View previous topic View next topic Go down

variable argument list

Post  Pulkit on Tue Aug 12, 2008 5:01 pm

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 5:27 pm

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

Dee2306

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

View user profile

Back to top Go down

use CSTDARG

Post  prani on Tue Aug 12, 2008 6: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

View previous topic View next topic Back to top


Post new topic   Reply to topic
Permissions of this forum:
You cannot reply to topics in this forum