Programming Problem
-
Hi I have to write a C program to take any number of arguments of any type from the user at runtime. I dont want to declare arrays of all the data types as that would be wastage of memory. Is there an efficient way to input any number of arguments from the user of any type? Please help as this is a part of my college project!! Thanks:confused:
-
Hi I have to write a C program to take any number of arguments of any type from the user at runtime. I dont want to declare arrays of all the data types as that would be wastage of memory. Is there an efficient way to input any number of arguments from the user of any type? Please help as this is a part of my college project!! Thanks:confused:
Try va_list a.k.a. variable-argument list. It's obvious, isn't it ? ;) HTH, Thomas
I am a signature virus! Help me spread and copy me to your sig!
-
Try va_list a.k.a. variable-argument list. It's obvious, isn't it ? ;) HTH, Thomas
I am a signature virus! Help me spread and copy me to your sig!
-
Here is en example, found at MSDN:
void main( void )
{
/* Call with 3 integers (-1 is used as terminator). */
printf( "Average is: %d\n", average( 2, 3, 4, -1 ) );/* Call with 4 integers. */
printf( "Average is: %d\n", average( 5, 7, 9, 11, -1 ) );/* Call with just -1 terminator. */
printf( "Average is: %d\n", average( -1 ) );
}int average( int first, ... )
{
int count = 0, sum = 0, i = first;
va_list marker;va_start( marker, first ); /* Initialize variable arguments. */
while( i != -1 )
{
sum += i;
count++;
i = va_arg( marker, int);
}
va_end( marker ); /* Reset variable arguments. */return( sum ? (sum / count) : 0 );
}HTH, Thomas
I am a signature virus! Help me spread and copy me to your sig!
-
Here is en example, found at MSDN:
void main( void )
{
/* Call with 3 integers (-1 is used as terminator). */
printf( "Average is: %d\n", average( 2, 3, 4, -1 ) );/* Call with 4 integers. */
printf( "Average is: %d\n", average( 5, 7, 9, 11, -1 ) );/* Call with just -1 terminator. */
printf( "Average is: %d\n", average( -1 ) );
}int average( int first, ... )
{
int count = 0, sum = 0, i = first;
va_list marker;va_start( marker, first ); /* Initialize variable arguments. */
while( i != -1 )
{
sum += i;
count++;
i = va_arg( marker, int);
}
va_end( marker ); /* Reset variable arguments. */return( sum ? (sum / count) : 0 );
}HTH, Thomas
I am a signature virus! Help me spread and copy me to your sig!
-
Hi thanks again :) But in your code you use all int arguments. Only the number of arguments is changing here. But I want to be able to multiple type of arguments!!! like float, char, long etc....possibly all of them!! is that possible with va_args?
IMHO, unfortunately, you have to know the type at compile-time. Regards, Thomas
I am a signature virus! Help me spread and copy me to your sig!
-
IMHO, unfortunately, you have to know the type at compile-time. Regards, Thomas
I am a signature virus! Help me spread and copy me to your sig!
This is trivial. Either that or I don't understand the problem. Your requirements: Store variable arguments of different types at runtime. Type is irrelevant. It will all be ascii at first. Then you detect your types and convert them. See, since YOU write the program, YOU decide the types. The arguments will be in char **argv and the number of them will be int argc. Write code to print out the value of argc. When that works, modify it to also print the argument strings, one by one. You are 90 percent finished. Now for the other 90 percent... :) Modify your code so that, instead of printing the arguments, you are PARSING them. First, decide which types you will support. Start with int and char strings. For each argument string, check each character. If you get to the end and find only digits, you have an int. Call atoi() on it and store it in a freshly newed int. Be careful of overflow - might wanna use longs and atol(). If it wasn't an int, it's a character string. New up a buffer and strcpy it over. To add another type, you must invent a way to detect the type and then you must find a way to convert the ascii string to your desired type.
-
Hi thanks again :) But in your code you use all int arguments. Only the number of arguments is changing here. But I want to be able to multiple type of arguments!!! like float, char, long etc....possibly all of them!! is that possible with va_args?
This may not be the way you want to go about it, but... You can write an overload function for each of the different variable types. So, if the function for integers is... (as was written in the previous answer) int average( int first, ... ) { int count = 0, sum = 0, i = first; va_list marker; va_start( marker, first ); /* Initialize variable arguments. */ while( i != -1 ) { sum += i; count++; i = va_arg( marker, int); } va_end( marker ); /* Reset variable arguments. */ return( sum ? (sum / count) : 0 ); } Then you just overload the function for other variable types such as: long average(long first, ...) { ... } double average(double first, ...) { ... } Etc. Then, when the call is made, the proper function for the variable type is called for you. Voila'.
-
Hi I have to write a C program to take any number of arguments of any type from the user at runtime. I dont want to declare arrays of all the data types as that would be wastage of memory. Is there an efficient way to input any number of arguments from the user of any type? Please help as this is a part of my college project!! Thanks:confused:
The question is somewhat confusing. By taking arguments from the user do you mean. 1) Reading program parameters from the command line. 2) Interpretting variable arguments passed to a subroutine from the caller. 3) Prompting for values to be put in. Various respondants seem to have interpretted it various ways. I guess 1) You'll probably loop through the parameters one by one. For each one you can try various conversions. The simplest way is probably to use sscanf. Try a format string like "%i%c" (tries to read the extra character to ensure the whole string was an integer). If it's not an integer try for a float. If it's not a float then treat is as a string. What you do with the arguments then really depends on the object of the excercise. You could, if you wanted, store them in a structure like -- struct myArg { short int type; union { float f_val; int i_val; char *string_val; } };