Thread: passing optioanl arguments to a function

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    662

    passing optioanl arguments to a function

    in my checkers game i have 2 nested for loops either initializing an array or copying one array to another. is there a way to declare a function to have optional arguments like printf can print a whole host of different things all of which are optional and up to the requirements of the call.
    coop

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,728
    Yes, there is. It's a variable argument list. Look it up, it's quite useful when you know what you're doing.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Apr 2019
    Posts
    662
    does the argument before the ... have to be an int if one of the variable arguments was a char could i do char ...

  4. #4
    Registered User
    Join Date
    Feb 2019
    Posts
    550
    if you want to mimic C++ "assumed" arguments behavior you can do something like below...

    Let's say we have a function that calculates the length of a 4 dimensions vector (x,y,z,w). But I want to pass only a 3d vector, assuming w=1:

    Code:
    // vector structure
    typedef struct { double x, y, z, w; } vec4_T;
      
    // macro, assumed .w=1.
    #define vec4_length(...) \
      vec4_length_( ( vec4_T ){ .w = 1.0, __VA_ARGS__ } )
      
    double vec4_length_( vec4_T v )
    { 
      if ( v.w != 0.0 && v.w != 1.0 )
      {
        v.x /= v.w;
        v.y /= v.w;
        v.z /= v.w;
        v.w = 1.0;
      }
      
      return sqrt( v.x * v.x + v.y * v.y + v.z * v.z );
    }
      
    // calling examples
    double l = vec4_length( .x=1, .y=1, .z=3 );
    double m = vec4_length( .x=1, .y=1, .z=1, .w=3 );
    If no designator is given, (x,y,z) will be 0... This is not a perfect mimic.

    Notice: ISO 9989 (C99 & C11) says (6.7.8 - about initializers) this is ok.

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,728
    Quote Originally Posted by cooper1200 View Post
    does the argument before the ... have to be an int if one of the variable arguments was a char could i do char ...
    No, it can be anything you want. But the known arguments must give you enough info to figure out how many arguments will follow. For example, printf uses the format string.
    Last edited by GReaper; 05-10-2019 at 12:00 PM.
    Devoted my life to programming...

  6. #6
    Registered User
    Join Date
    Apr 2019
    Posts
    662
    so i could do my_func(int x. int y, int argument_count, char ...)
    coop

  7. #7
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,728
    Yes, but without the "char" bit. You don't specify a type for the optional arguments, the va_arg macro does that on the fly.
    Devoted my life to programming...

  8. #8
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    657
    One thing to also note: if your optional or variadic arguments may be smaller than int (such as char or short), they will be converted to int when the function is called, so you will need to retrieve them from the argument list with va_arg(ap, int) rather than with va_arg(ap, char) or va_arg(ap, short).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing arguments to a function
    By cfanatic in forum C Programming
    Replies: 2
    Last Post: 07-01-2012, 06:50 AM
  2. Help passing arguments to function
    By HAssan in forum C Programming
    Replies: 2
    Last Post: 11-26-2007, 02:15 PM
  3. Passing arguments to another function
    By Wiretron in forum C Programming
    Replies: 2
    Last Post: 12-24-2006, 05:57 AM
  4. need function help, passing arguments
    By infernosnow in forum Game Programming
    Replies: 18
    Last Post: 07-18-2006, 02:45 AM
  5. Passing arguments to function...
    By alvifarooq in forum C++ Programming
    Replies: 8
    Last Post: 09-24-2004, 12:50 PM

Tags for this Thread