By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,067 Members | 1,849 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,067 IT Pros & Developers. It's quick & easy.

functions with variable length argument lists

P: 82
I found many pages about my problem(topic title) and i understand how it works
and all the examples
but after i found an example which i want to explain me
look especially in function maxof and the comments
[PHP]#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>

main(){
f();

exit(EXIT SUCCESS)
}

void f(void){
int i=5;
int j[256];
j[42]=24;
printf("%d\n", maxof(3, i, j[42], 0);

int maxof(int n_args, ... ){ /*what is n-args */
/* i understand that n_args is one of the numbers(the first) that we pass in the function to find the max*/
register int i;
int max, a;
va_list ap;

max=va_arg(ap,int)/*why set max with i(the second argument) and not with the first which is n_args or 3*/
for(i=2; i <=n_args; i++){
if((a = va_arg(ap, int)) > max
max = a;
}
va_end(ap);
return max;
}
[/PHP]
I think that we don't check if the first argument is max, because we set max in the beginning with the second argument and we check the rest numbers from i to 0.
Where is my mistake?Maybe don't understand the meaning of n_args
Aug 10 '07 #1
Share this Question
Share on Google+
5 Replies


Expert 10K+
P: 11,448
The parameter n_args is simply the number of arguments that should be considered
in determining the maximum number. As in:

Expand|Select|Wrap|Line Numbers
  1. int max= f(3, -7, 0, 8);
  2.  
Three numbers are to be considered: -7, 0 and 8. This way you don't need a
special 'sentinel' value such as -9999 (ugly) to indicate the last argument.
The disadvantage is that you can also do this by mistake:

Expand|Select|Wrap|Line Numbers
  1. int max= f(2, -7, 0, 8);
  2.  
The number 8 is not considered one of the list for which a maximum must be found.

kind regards,

Jos
Aug 10 '07 #2

P: 82
The disadvantage is that you can also do this by mistake:

Expand|Select|Wrap|Line Numbers
  1. int max= f(2, -7, 0, 8);
  2.  
The number 8 is not considered one of the list for which a maximum must be found.

kind regards,

Jos
Why number 8 ?As you say the first argument(2 in the example) n_args is simply the number of arguments that should be considered.So number 2 is not considered one of the list for which a maximum must be found and not 8.Am i wrong??
Aug 10 '07 #3

Expert 10K+
P: 11,448
Why number 8 ?As you say the first argument(2 in the example) n_args is simply the number of arguments that should be considered.So number 2 is not considered one of the list for which a maximum must be found and not 8.Am i wrong??
I'm afraid I didn't express myself clear enough: the first parameter of that function
indicates the number of following parameters for which the maximum must be found. So:

Expand|Select|Wrap|Line Numbers
  1. int max= f(2, -7, 0, 8)
  2.  
Tells the function to take the first two arguments *following the very first argument*,
i.e. -7 and 0 and find the maximum of them. Number 8 is not considered.

kind regards,

Jos
Aug 10 '07 #4

P: 82
Thank you very very much ,now i understand it
Aug 10 '07 #5

Expert 10K+
P: 11,448
Thank you very very much ,now i understand it
Good, you're welcome of course. In general: using C there is no way for a varargs
function to tell how many parameters were actually passed to it, i.e. it has to trust
the caller. I can't remember if C++ could do any better than that ...

kind regards,

Jos
Aug 10 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.