Pointer-to-function with variable argument number

Discussion in 'C Programming' started by Andrej Prsa, Mar 4, 2004.

  1. Andrej Prsa

    Andrej Prsa Guest

    Hi!

    Why do I get a warning about incompatible pointer type if I try to assign
    a pointer to the function with variable argument number:

    int func (int argc, ...) ,

    but everything is ok when I'm using a simple function like:

    int func (int argc, char *whatever)

    Can't I point to the function with variable number of arguments?

    Best wishes,

    Andrej
     
    Andrej Prsa, Mar 4, 2004
    #1
    1. Advertising

  2. Andrej Prsa

    Leor Zolman Guest

    On Thu, 4 Mar 2004 01:15:11 +0100, Andrej Prsa <>
    wrote:

    >Hi!
    >
    >Why do I get a warning about incompatible pointer type if I try to assign
    >a pointer to the function with variable argument number:
    >
    > int func (int argc, ...) ,
    >
    >but everything is ok when I'm using a simple function like:
    >
    > int func (int argc, char *whatever)
    >
    >Can't I point to the function with variable number of arguments?
    >
    >Best wishes,
    >
    >Andrej


    Things just have to match. To point to a function with a variable number of
    args, use a pointer to a function that takes a variable number of args:

    int foo(...);
    int bar(int i);

    int main()
    {
    int (*pf)(...);
    int (*pb)(int);

    pf = foo; // OK
    pf = bar; // error
    pb = bar; // OK
    pb = foo; // error
    return 0;
    }


    Leor Zolman
    BD Software

    www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
    C++ users: Download BD Software's free STL Error Message
    Decryptor at www.bdsoft.com/tools/stlfilt.html
     
    Leor Zolman, Mar 4, 2004
    #2
    1. Advertising

  3. Andrej Prsa

    Andrej Prsa Guest

    > Things just have to match. To point to a function with a variable number
    > of args, use a pointer to a function that takes a variable number of
    > args:
    >
    > int foo(...);
    > int bar(int i);
    >
    > int main()
    > {
    > int (*pf)(...);
    > int (*pb)(int);
    >
    > pf = foo; // OK
    > pf = bar; // error
    > pb = bar; // OK
    > pb = foo; // error
    > return 0;
    > }


    Great, this makes a lot of sense! Thanks,

    Andrej
     
    Andrej Prsa, Mar 4, 2004
    #3
  4. Andrej Prsa

    Chris Torek Guest

    In article <news:>
    Leor Zolman <> writes:
    >Things just have to match. To point to a function with a variable number of
    >args, use a pointer to a function that takes a variable number of args:


    Right, but:

    >int foo(...);


    this type does not exist in C. Variable-arguments functions must
    have at least one initial fixed argument. You might make this:

    int foo(int, ...);

    if it takes some number of "int" parameters ending with a -1, even
    if the "some number" is allowed to be zero (so that the first int
    is just -1).

    (I think this is a broken part of the C Standards, but it is still
    there and we are stuck with it. It is not a serious flaw, just a
    minor one, like the lack of zero-sized objects.)
    --
    In-Real-Life: Chris Torek, Wind River Systems
    Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
    email: forget about it http://web.torek.net/torek/index.html
    Reading email is like searching for food in the garbage, thanks to spammers.
     
    Chris Torek, Mar 4, 2004
    #4
  5. Andrej Prsa

    Leor Zolman Guest

    On 4 Mar 2004 05:33:23 GMT, Chris Torek <> wrote:

    >In article <news:>
    >Leor Zolman <> writes:
    >>Things just have to match. To point to a function with a variable number of
    >>args, use a pointer to a function that takes a variable number of args:

    >
    >Right, but:
    >
    >>int foo(...);

    >
    >this type does not exist in C. Variable-arguments functions must
    >have at least one initial fixed argument. You might make this:
    >
    > int foo(int, ...);
    >
    >if it takes some number of "int" parameters ending with a -1, even
    >if the "some number" is allowed to be zero (so that the first int
    >is just -1).
    >
    >(I think this is a broken part of the C Standards, but it is still
    >there and we are stuck with it. It is not a serious flaw, just a
    >minor one, like the lack of zero-sized objects.)


    Thanks! I wasn't aware of that, and when testing I must have forgotten to
    give the Comeau compiler the --c99 option. Even if you compile a file with
    a .c extension, that compiler (which happens to be my favorite) defaults to
    C++ mode. Arghhh. I may have to just modify my 4NT compile script to add
    that option automatically when it detects I'm compiling with Comeau...
    -leor


    Leor Zolman
    BD Software

    www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
    C++ users: Download BD Software's free STL Error Message
    Decryptor at www.bdsoft.com/tools/stlfilt.html
     
    Leor Zolman, Mar 4, 2004
    #5
  6. Andrej Prsa

    Derk Gwen Guest

    Andrej Prsa <> wrote:
    # Hi!
    #
    # Why do I get a warning about incompatible pointer type if I try to assign
    # a pointer to the function with variable argument number:
    #
    # int func (int argc, ...) ,
    #
    # but everything is ok when I'm using a simple function like:
    #
    # int func (int argc, char *whatever)
    #
    # Can't I point to the function with variable number of arguments?

    If you want to assign functions with various arguments, you can use a
    cast or old-style functions. Doing so is, of course, less safe.

    typedef void (*func)(void);
    typedef void (*old)();

    void a(b,c) int b,c; {;}
    void w(x,y,z) double x,y,z; {;}
    int p(int q,void *r) {;}

    void t(void) {
    func P; old Q;
    P = (func)a; /*casts*/
    P = (func)w;
    P = (func)p;
    Q = a; /*old style functions*/
    Q = w;
    }

    --
    Derk Gwen http://derkgwen.250free.com/html/index.html
    Haven't you ever heard the customer is always right?
     
    Derk Gwen, Mar 4, 2004
    #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. Ben Kial
    Replies:
    1
    Views:
    718
    Eric Enright
    Nov 15, 2004
  2. S?ren Gammelmark
    Replies:
    1
    Views:
    1,984
    Eric Sosman
    Jan 7, 2005
  3. Vijai Kalyan
    Replies:
    4
    Views:
    745
    Vijai Kalyan
    Nov 8, 2005
  4. Reckoner
    Replies:
    11
    Views:
    735
    Steven D'Aprano
    Jan 19, 2009
  5. AikidoGuy
    Replies:
    11
    Views:
    593
    Seebs
    Nov 21, 2011
Loading...

Share This Page