optional arguments..

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jason

    optional arguments..

    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!

  • Peter Nilsson

    #2
    Re: optional arguments..

    Jason wrote:[color=blue]
    > 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?[/color]

    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.
    [color=blue]
    > 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;
    > }[/color]

    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

    Comment

    • Jason

      #3
      Re: optional arguments..

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

      Cheers!

      Comment

      • jacob navia

        #4
        Re: optional arguments..

        Jason wrote:[color=blue]
        > 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!
        >[/color]
        The lcc-win32 compiler implements this as an extension. The syntax is
        the same as you wrote.


        Comment

        • Default User

          #5
          Re: optional arguments..

          Jason wrote:
          [color=blue]
          > Thanks Peter. To be honest I haven't programmed in ages and was
          > wondering if this was possible in C at all.[/color]


          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.

          Comment

          • Peter Shaggy Haywood

            #6
            Re: optional arguments..

            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!
            [color=blue]
            >I'm new to C .. have a very basic question .. hope someone has the
            >patience to help![/color]

            No worries! We're very patient with people who are on-topic and
            willing to learn.
            [color=blue]
            >I want to declare a function that has optional parameters and I want to
            >initialize the non supplied parameters .. how do I do this?[/color]

            It wouldn't be too hard, using a variadic function. You'd need
            another parameter, though, representing the number of variadic
            arguments passed.
            [color=blue]
            >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;
            >}[/color]

            #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[i] = va_arg(ap, int);
            }

            va_end(ap);

            /* Do something. */

            return 0;
            }

            --

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


            "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 "technicall y correct" English; but since when was rock & roll "technicall y correct"?

            Comment

            Working...