optional arguments..

Discussion in 'C Programming' started by Jason, Apr 28, 2006.

  1. Jason

    Jason Guest

    Hi there,

    I'm new to C .. have a very basic question .. hope someone has the
    patience to help!

    I want to declare a function that has optional parameters and I want to
    initialize the non supplied parameters .. how do I do this?

    eg:

    // this obviously isn't allowed .. but need to acheive something
    similar
    int function foo (int a=0, int b=0, int c=0, int d=0) {

    // do something
    return 0;
    }

    // I want to be able to call this function with any or all the
    parameters

    foo (2, 5,); // in this case a=2, b=5, c should be 0 and d should be 0


    How do I accomplish this?


    Thanks a bunch guys!
     
    Jason, Apr 28, 2006
    #1
    1. Advertising

  2. Jason wrote:
    > Hi there,
    >
    > I'm new to C .. have a very basic question .. hope someone has the
    > patience to help!
    >
    > I want to declare a function that has optional parameters and I want to
    > initialize the non supplied parameters .. how do I do this?


    You can't in C. You can mimic it to some extent (e.g. va_xxxx macros in
    <stdarg.h>), but it isn't pretty or recommended, especially for
    beginners.

    > eg:
    >
    > // this obviously isn't allowed .. but need to acheive something
    > similar
    > int function foo (int a=0, int b=0, int c=0, int d=0) {
    >
    > // do something
    > return 0;
    > }


    If you really want to do this, try C++.

    If you need to use C only, then you should elaborate on what your
    problem actually is, rather than state what you think is the solution
    to an as yet unspecified problem.

    --
    Peter
     
    Peter Nilsson, Apr 28, 2006
    #2
    1. Advertising

  3. Jason

    Jason Guest

    Thanks Peter. To be honest I haven't programmed in ages and was
    wondering if this was possible in C at all.

    Cheers!
     
    Jason, Apr 28, 2006
    #3
  4. Jason

    jacob navia Guest

    Jason wrote:
    > Hi there,
    >
    > I'm new to C .. have a very basic question .. hope someone has the
    > patience to help!
    >
    > I want to declare a function that has optional parameters and I want to
    > initialize the non supplied parameters .. how do I do this?
    >
    > eg:
    >
    > // this obviously isn't allowed .. but need to acheive something
    > similar
    > int function foo (int a=0, int b=0, int c=0, int d=0) {
    >
    > // do something
    > return 0;
    > }
    >
    > // I want to be able to call this function with any or all the
    > parameters
    >
    > foo (2, 5,); // in this case a=2, b=5, c should be 0 and d should be 0
    >
    >
    > How do I accomplish this?
    >
    >
    > Thanks a bunch guys!
    >

    The lcc-win32 compiler implements this as an extension. The syntax is
    the same as you wrote.

    http://www.cs.virginia.edu/~lcc-win32
     
    jacob navia, Apr 28, 2006
    #4
  5. Jason

    Default User Guest

    Jason wrote:

    > Thanks Peter. To be honest I haven't programmed in ages and was
    > wondering if this was possible in C at all.



    Please review the information below.



    Brian
    --
    Please quote enough of the previous message for context. To do so from
    Google, click "show options" and use the Reply shown in the expanded
    header.
     
    Default User, Apr 28, 2006
    #5
  6. Groovy hepcat Jason was jivin' on 27 Apr 2006 18:28:25 -0700 in
    comp.lang.c.
    optional arguments..'s a cool scene! Dig it!

    >I'm new to C .. have a very basic question .. hope someone has the
    >patience to help!


    No worries! We're very patient with people who are on-topic and
    willing to learn.

    >I want to declare a function that has optional parameters and I want to
    >initialize the non supplied parameters .. how do I do this?


    It wouldn't be too hard, using a variadic function. You'd need
    another parameter, though, representing the number of variadic
    arguments passed.

    >eg:
    >
    >// this obviously isn't allowed .. but need to acheive something
    >similar
    >int function foo (int a=0, int b=0, int c=0, int d=0) {
    >
    > // do something
    > return 0;
    >}


    #include <stdarg.h>

    int foo(size_t nargs, ...)
    {
    int a = 0, b = 0, c = 0, d = 0;
    int *arr[4];
    va_list ap;
    size_t i;

    arr[0] = &a;
    arr[1] = &b;
    arr[2] = &c;
    arr[3] = &d;

    va_start(ap, nargs);

    for(i = 0; i < nargs; i++)
    {
    *arr = va_arg(ap, int);
    }

    va_end(ap);

    /* Do something. */

    return 0;
    }

    --

    Dig the even newer still, yet more improved, sig!

    http://alphalink.com.au/~phaywood/
    "Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
    I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
     
    Peter Shaggy Haywood, Apr 30, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul Probert

    optional arguments in c extensions

    Paul Probert, Oct 24, 2003, in forum: Python
    Replies:
    1
    Views:
    265
    Paul Probert
    Oct 24, 2003
  2. Roger Upole
    Replies:
    0
    Views:
    398
    Roger Upole
    Dec 1, 2003
  3. Ezequiel, Justin

    optional arguments to pattern.findall()

    Ezequiel, Justin, May 5, 2004, in forum: Python
    Replies:
    1
    Views:
    345
    Peter Otten
    May 5, 2004
  4. Rob Hoelz

    Getting at optional arguments

    Rob Hoelz, Jan 15, 2007, in forum: C Programming
    Replies:
    6
    Views:
    356
    Default User
    Jan 16, 2007
  5. braver
    Replies:
    3
    Views:
    460
    Steven Bethard
    Nov 12, 2007
Loading...

Share This Page