Thread: variable argument lists

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    4

    variable argument lists

    Hi

    I am looking for a function which should have the same behaviour of in-build printf function.

    I tried following code which i got it thru google but it ends up with compilation error:

    Code:
    #include <stdio.h>
    #include <stdarg.h>			/* va_list, va_arg, va_end	*/
    
    int set(char *item, int, num, ...);
    
    main()
       {
       char *item="pear";
    
       (set (item,4, "apple", "pear", "banana", "grape") )
    	? printf ("%s found\n", item) : printf("%s not found\n", item);
    
       }
    
    int set(char *item, int num,...)
       {
       va_list ap;				/* define 'ap' It acts as a pointer 
    					 * to the undefined variables.	*/
       int ok=0;
       int inc=0;
       va_start(ap, num);			/* seed 'ap'			*/
    
       do {
          if ( item == va_arg(ap, char *)) ok=1;
          } while ( ok==0 && ++inc < num);
    
       va_end(ap);				/* tidy up.			*/
       return (ok);
       }
    And i get following errors:

    TestArgs2.c:14: error: syntax error before "num"
    TestArgs2.c:30: error: conflicting types for `set'
    TestArgs2.c:30: error: a parameter list with an ellipsis can't match an empty parameter name list declaration
    TestArgs2.c:14: error: previous declaration of `set'


    Any help is highly appreciated

    Thank you all
    Ravi

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Look:
    Code:
    int set(char *item, int num, ...);
    Lose the comma and try again.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Registered User
    Join Date
    May 2005
    Posts
    4

    Thanks

    Oops!!!!
    I didnt notice it.
    Anyway thanks for the help. It worked.



Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Server Help
    By lautarox in forum C Programming
    Replies: 146
    Last Post: 09-24-2008, 06:32 PM
  2. Passing Through A Variable Argument List
    By SMurf in forum C Programming
    Replies: 6
    Last Post: 04-14-2007, 11:12 AM
  3. Variable Argument List Passing
    By Orborde in forum C++ Programming
    Replies: 2
    Last Post: 05-10-2005, 08:42 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Nested loop frustration
    By caroundw5h in forum C Programming
    Replies: 14
    Last Post: 03-15-2004, 09:45 PM