Thread: variable number of arguments

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    11

    Exclamation variable number of arguments

    I cant understand this code.. Pls help..

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdarg.h>
    
    
    int findmax(int, ...);
    int main()
    {
      int max;
      
      max = findmax(5, 23, 15, 1, 92, 50);
      printf("maximum = %d\n", max);
      
      max = findmax (3, 100, 300, 29);
      printf("maximum = %d\n", max);
      
      system("PAUSE");    
      return 0;
    }
    
    
    int findmax (int tot_num, ... )   // From here
    {
        int max, count, num;
        
        va_list ptr;
        va_start(ptr, tot_num);
        max = va_arg(ptr, int);
        
        for(count = 1; count < tot_num; count++)
        {
                  num = va_arg(ptr, int);
                  if(num> max)
                  max = num;   //especially this
        }
        
        return (max);
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Have you read the tutorial on variable argument lists?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2012
    Posts
    11
    if(num> max)
    max = num;

    Totally confused with these two lines (33, 34)..

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The idea is that max store the maximum value among those that have been checked. So, if the current value that you are checking is greater than this max, then it must be the new max.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jun 2012
    Posts
    11
    line 30

    why is "count < tot_num" ?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by newC72
    why is "count < tot_num" ?
    Have you read the tutorial on variable argument lists?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by newC72 View Post
    line 30

    why is "count < tot_num" ?
    Because the argument that follows tot_num viz., 23 in this case
    Code:
    findmax(5, 23, 15, 1, 92, 50)
    has been picked off by the piece of code below before the for loop starts:
    Code:
    max = va_arg(ptr, int);
    And as laserlight recommends, you'd be better off by going through the tutorial on this site.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function with variable number of arguments
    By andrewg in forum C Programming
    Replies: 9
    Last Post: 12-18-2010, 09:19 PM
  2. variable number of arguments for a function or a macro
    By mynickmynick in forum C Programming
    Replies: 5
    Last Post: 05-19-2010, 11:35 AM
  3. Variable number of arguments in function.
    By kamoda_pawel in forum C Programming
    Replies: 1
    Last Post: 01-18-2007, 07:18 AM
  4. Variable number of arguments
    By dit6a9 in forum Windows Programming
    Replies: 3
    Last Post: 08-10-2004, 08:58 AM
  5. Functions with a variable number of arguments
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2002, 01:12 AM