what's this?

B

bakzam

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

bakzam

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)

sorry for the typo, it is:

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

bakzam

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

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

func(-1);

and about the macro INTERFACE_FINDER(-1)
 
I

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

bakzam

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

The original code you posted was nonsense.

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

bakzam

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

Ian Collins

*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;
 
E

Eric Sosman

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:

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

Ian Collins

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)
As I said elsethread, you don't show ENTRY_IS_OFFSET.
 
K

Keith Thompson

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

Barry Schwarz

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
 
R

Richard Heathfield

Keith Thompson said:

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.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top