what's this?

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

  1. 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)
    --------------

    Isn't INTERFACE_FINDER(-1) a call to a function? How is it an offset?
    , Jun 24, 2007
    #1
    1. Advertising

  2. Guest

    On Jun 25, 1:46 am, wrote:
    > 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)
    > --------------
    >
    > Isn't INTERFACE_FINDER(-1) a call to a function? How is it an offset?


    sorry for the typo, it is:

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

  3. Ian Collins Guest

    wrote:
    > sorry for the typo, it is:
    >
    > typedef int (*func)(filo, int);
    >

    It's a pointer to a function returning int which has a filo and an int
    as its parameters.

    --
    Ian Collins.
    Ian Collins, Jun 24, 2007
    #3
  4. Guest

    On Jun 25, 1:58 am, Ian Collins <> wrote:
    > wrote:
    > > sorry for the typo, it is:

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

    >
    > It's a pointer to a function returning int which has a filo and an int
    > as its parameters.
    >
    > --
    > Ian Collins.


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

    func(-1);

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

    wrote:
    > On Jun 25, 1:58 am, Ian Collins <> wrote:
    >> wrote:
    >>> sorry for the typo, it is:
    >>> typedef int (*func)(filo, int);

    >> It's a pointer to a function returning int which has a filo and an int
    >> as its parameters.
    >>
    >> --
    >> Ian Collins.

    >
    > that's not what I asked!! I wanted to know about the statement
    >
    > func(-1);
    >
    > and about the macro INTERFACE_FINDER(-1)
    >

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

    The original code you posted was nonsense.

    --
    Ian Collins.
    Ian Collins, Jun 24, 2007
    #5
  6. Guest

    On Jun 25, 2:16 am, Ian Collins <> wrote:
    > wrote:
    > > On Jun 25, 1:58 am, Ian Collins <> wrote:
    > >> wrote:
    > >>> sorry for the typo, it is:
    > >>> typedef int (*func)(filo, int);
    > >> It's a pointer to a function returning int which has a filo and an int
    > >> as its parameters.

    >
    > >> --
    > >> Ian Collins.

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

    >
    > > func(-1);

    >
    > > and about the macro INTERFACE_FINDER(-1)

    >
    > Which you don't show, so how can anyone give yo an answer?
    >
    > The original code you posted was nonsense.
    >
    > --
    > Ian Collins.


    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.
    , Jun 24, 2007
    #6
  7. Guest

    On Jun 25, 2:21 am, wrote:
    > On Jun 25, 2:16 am, Ian Collins <> wrote:
    >
    >
    >
    > > wrote:
    > > > On Jun 25, 1:58 am, Ian Collins <> wrote:
    > > >> wrote:
    > > >>> sorry for the typo, it is:
    > > >>> typedef int (*func)(filo, int);
    > > >> It's a pointer to a function returning int which has a filo and an int
    > > >> as its parameters.

    >
    > > >> --
    > > >> Ian Collins.

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

    >
    > > > func(-1);

    >
    > > > and about the macro INTERFACE_FINDER(-1)

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

    >
    > > The original code you posted was nonsense.

    >
    > > --
    > > Ian Collins.

    >
    > 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.


    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)
    ----------------

    Don Box uses this to automate (table driven process) to implement
    QueryInterface.
    , Jun 24, 2007
    #7
  8. Ian Collins Guest

    wrote:
    > On Jun 25, 2:16 am, Ian Collins <> wrote:
    >> wrote:
    >>> On Jun 25, 1:58 am, Ian Collins <> wrote:
    >>>> wrote:
    >>>>> sorry for the typo, it is:
    >>>>> typedef int (*func)(filo, int);
    >>>> It's a pointer to a function returning int which has a filo and an int
    >>>> as its parameters.
    >>>> --
    >>>> Ian Collins.
    >>> that's not what I asked!! I wanted to know about the statement
    >>> func(-1);
    >>> and about the macro INTERFACE_FINDER(-1)

    >> Which you don't show, so how can anyone give yo an answer?
    >>
    >> The original code you posted was nonsense.
    >>

    *Please* don't quote signatures.
    >
    > 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.
    >

    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.
    Ian Collins, Jun 24, 2007
    #8
  9. Eric Sosman Guest

    wrote:
    > On Jun 25, 2:21 am, wrote:
    >> On Jun 25, 2:16 am, Ian Collins <> wrote:
    >>
    >>
    >>
    >>> wrote:
    >>>> On Jun 25, 1:58 am, Ian Collins <> wrote:
    >>>>> wrote:
    >>>>>> sorry for the typo, it is:
    >>>>>> typedef int (*func)(filo, int);
    >>>>> It's a pointer to a function returning int which has a filo and an int
    >>>>> as its parameters.
    >>>>> --
    >>>>> Ian Collins.
    >>>> that's not what I asked!! I wanted to know about the statement
    >>>> func(-1);
    >>>> and about the macro INTERFACE_FINDER(-1)
    >>> Which you don't show, so how can anyone give yo an answer?
    >>> The original code you posted was nonsense.
    >>> --
    >>> Ian Collins.

    >> 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.


    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.

    > 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);


    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."

    > //pseudo-function to indicate entry is just an offset
    > #define ENTRY_IS_OFFSET INTERFACE_FINDER(-1)


    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
    acm-dot-org.invalid
    Eric Sosman, Jun 24, 2007
    #9
  10. Ian Collins Guest

    wrote:
    >
    > 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)
    > ----------------
    >
    > Don Box uses this to automate (table driven process) to implement
    > QueryInterface.
    >

    As I said elsethread, you don't show ENTRY_IS_OFFSET.

    --
    Ian Collins.
    Ian Collins, Jun 24, 2007
    #10
  11. writes:
    > On Jun 25, 1:46 am, wrote:
    >> 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?

    [snip]

    > sorry for the typo, it is:
    >
    > typedef int (*func)(filo, int);


    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 (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Jun 24, 2007
    #11
  12. On Sun, 24 Jun 2007 20:48:12 -0000, wrote:

    >On Jun 25, 1:46 am, wrote:
    >> 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)
    >> --------------
    >>
    >> Isn't INTERFACE_FINDER(-1) a call to a function? How is it an offset?

    >
    >sorry for the typo, it is:
    >
    >typedef int (*func)(filo, int);


    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:

    <snip>

    > 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.


    Or the language he's asking about is not the same as the language he is
    using.

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
    Richard Heathfield, Jun 25, 2007
    #13
    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.

Share This Page