473,396 Members | 1,975 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

functions with variable length argument lists

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
5 1738
JosAH
11,448 Expert 8TB
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
kalar
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
JosAH
11,448 Expert 8TB
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
kalar
82
Thank you very very much ,now i understand it
Aug 10 '07 #5
JosAH
11,448 Expert 8TB
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

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

Similar topics

99
by: David MacQuigg | last post by:
I'm not getting any feedback on the most important benefit in my proposed "Ideas for Python 3" thread - the unification of methods and functions. Perhaps it was buried among too many other less...
1
by: optimistx | last post by:
How to build or find an object browser for javascript? E.g. Delphi integrated developement environment ('ide') offers a very practical object browser for Pascal language. When typing a name with...
4
by: Peter Salzman | last post by:
Hi all, Since PHP is a (weakly) typed language, is it possible to overload functions based on the type of their argument? Also, are there function pointers in PHP? Here's why I'm asking. ...
8
by: Dennis Gearon | last post by:
Is it possible to write functions for PL/C that have variable argument lists? I.E. UPPER( arg ) UPPER( arg, encoding_list ) LOWER( arg ) LOWER( arg, encoding_list ) SUBSTR( arg, arg, arg )
13
by: some one | last post by:
I know that C++ lets you overload functions, but how do I overload functions in C?
9
by: Gibby Koldenhof | last post by:
Hiya, Terrible subject but I haven't got a better term at the moment. I've been building up my own library of functionality (all nice conforming ISO C) for over 6 years and decided to adopt a...
18
by: SDZ | last post by:
Could somebody explain in simple forms, what is/are the difference(s) between - scanf and sscanf and ssscanf - printf and sprintf - open and fopen Thanks in advance, SDZ
5
by: Jonathan Burd | last post by:
Greetings everyone, I wrote a function to learn about variable-length argument lists. I wonder if there is a better way to detect the end of the argument list than using a sentinel value like...
6
by: CptDondo | last post by:
How do you declare a function with 0 or mroe arguments? I have a bunch of functions like this: void tc_cm(int row, int col); void tc_do(void); void tc_DO(int ln); and I am trying to ...
19
by: Spiros Bousbouras | last post by:
Every time I've seen an example of a variable argument list function its functionality was to print formatted output. Does anyone have examples where the function is not some variation of printf ?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.