
Pointers to Structure that contains pointer to Function
On Thu, 18 Mar 1999 17:37:41 GMT, "James G"
Quote:
> My compiler is doing something I don't understand.
> If I have a structure:
> struct S_type
> {
> ....
> (void)(*f)(void);
> ....
> } S;
> and a pointer to struct S:
> struct S * pS;
struct S_type * pS;
Quote:
> and if pS= &S;
> then I can call function f in two ways:
> (*(pS->f))();
Why not simply
pS->f();
It is perhaps not well known that the operator () applies to pointers
to functions and not to functions. If g is a function then in the call
g() 'g' would be implicitly converted to a pointer to g.
Thus - formally - you are dereferencing pS->f to yield a "function"
and then this function will be implicitly re-converted to a pointer to
the function (which f already is) because the () operator is applied
to it. Your code is correct, but it is an unnecessary NOP.
Quote:
> or
> pS.f();
> My question is why does this last method work? I though the dot
> operator only worked with the name of a structure, not a pointer
> to it?
If your compiler allows this it is not an ANSI C compiler.
(*pS).f()
would be OK, though, and is identical to pS->f()
Regards
Horst
--