what's this?

Discussion in 'C Programming' started by bakzam, Jun 24, 2007.

  1. bakzam

    bakzam Guest

    struct filo{
    float p;
    };

    typedef int (*func)filo, int);

    int main(void){
    func(-1); //Hmmm. what's this?
    return 0;
    }

    why is func(-1) working?

    Actually, I found this (equivalent code) in DON BOX's book

    --------------------
    typedef HRESULT (*INTERFACE_FINDER) (void *pThis, DWORD dwData, REFIID
    riid, void **ppv);

    //pseudo-function to indicate entry is just an offset
    #define ENTRY_IS_OFFSET INTERFACE_FINDER(-1)
     
    bakzam, Jun 24, 2007
    #1
    1. Advertisements

  2. bakzam

    bakzam Guest

    sorry for the typo, it is:

    typedef int (*func)(filo, int);
     
    bakzam, Jun 24, 2007
    #2
    1. Advertisements

  3. bakzam

    Ian Collins Guest

    It's a pointer to a function returning int which has a filo and an int
    as its parameters.
     
    Ian Collins, Jun 24, 2007
    #3
  4. bakzam

    bakzam Guest

    that's not what I asked!! I wanted to know about the statement

    func(-1);

    and about the macro INTERFACE_FINDER(-1)
     
    bakzam, Jun 24, 2007
    #4
  5. bakzam

    Ian Collins Guest

    Which you don't show, so how can anyone give yo an answer?

    The original code you posted was nonsense.
     
    Ian Collins, Jun 24, 2007
    #5
  6. bakzam

    bakzam Guest

    OK, But what about

    func(-1);

    It is not non-sense. There has to be a reason as to why is it not
    giving error. I think it is creating a constant function pointer, or
    something like that. I am not sure, so have asked this question.
     
    bakzam, Jun 24, 2007
    #6
  7. bakzam

    bakzam Guest

    Moreover, I wanted to know what can someone interpret from the
    following two lines:

    ---------------------
    typedef HRESULT (*INTERFACE_FINDER) (void *pThis, DWORD dwData, REFIID
    riid, void **ppv);

    //pseudo-function to indicate entry is just an offset
    #define ENTRY_IS_OFFSET INTERFACE_FINDER(-1)
     
    bakzam, Jun 24, 2007
    #7
  8. bakzam

    Ian Collins Guest

    *Please* don't quote signatures.
    It is nonsense, you have declared func as a type, you can't initialise a
    type that way in C (are you thinking C++?).

    You could write

    func f = (func)-1;
     
    Ian Collins, Jun 24, 2007
    #8
  9. bakzam

    Eric Sosman Guest

    I can think of two reasons why it doesn't produce
    an error: First, the compiler may be broken. Second,
    you may have mis-transcribed the code.
    This says: "INTERFACE_FINDER is a pointer to functions
    that take four arguments of type void*, DWORD, REFIID, and void**,
    and that return a value of type HRESULT."
    This says "Wherever the identifier ENTRY_IS_OFFSET appears
    in the source, replace it with the five-token sequence

    INTERFACE_FINDER
    (
    -
    1
    )

    .." Since the expanded token sequence would be nonsense, I
    suspect another transcription error (are you sure there aren't
    more parentheses?) or else a #define INTERFACE_FINDER somewhere
    else in the code.
     
    Eric Sosman, Jun 24, 2007
    #9
  10. bakzam

    Ian Collins Guest

    As I said elsethread, you don't show ENTRY_IS_OFFSET.
     
    Ian Collins, Jun 24, 2007
    #10
  11. Ok, so your code is:

    struct filo{
    float p;
    };

    typedef int (*func)(filo, int);

    int main(void){
    func(-1); //Hmmm. what's this?
    return 0;
    }

    When I try to compile it, I get a parse error on "func(-1);".

    If it works for you, then it's because the code you posted is not the
    same as the code you compiled.

    If you post code here, *always* copy-and-paste it; never attempt to
    re-type it. There was at least one error in the code you originally
    posted (the one you corrected in a followup); there must be more
    errors than that.
     
    Keith Thompson, Jun 24, 2007
    #11
  12. If filo were a type, this would define the type func as being a
    synonym for the type pointer to function taking two arguments (first
    of type filo, second of type int) and returning int. Since filo is
    not a type, this is just a syntax error.

    Define what you mean by working. Is it merely the absence of a
    diagnostic? If so, then maybe you can raise the warning level on your
    system. Or maybe your compiler has an error. Have you looked at the
    generated code to see what it does, if anything. Have you stepped
    through it with a debugger.

    And if your compiler accepts the typedef, you are operating in the
    realm of compiler extensions. Maybe func(-1) is another such
    extension. You should ask in a group where your compiler is topical.


    Remove del for email
     
    Barry Schwarz, Jun 25, 2007
    #12
  13. Keith Thompson said:

    Or the language he's asking about is not the same as the language he is
    using.
     
    Richard Heathfield, Jun 25, 2007
    #13
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.