Pointers to Structure that contains pointer to Function 
Author Message
 Pointers to Structure that contains pointer to Function

Hello,
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;

and if   pS= &S;

then I can call function f in two ways:

     (*(pS->f))();
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?
thanks for any insight,
James

--



Mon, 03 Sep 2001 03:00:00 GMT  
 Pointers to Structure that contains pointer to Function

   If I have a structure:
     struct S_type
         {
         ....
         (void)(*f)(void);
         ....
         } S;

   and a pointer to struct S:
       struct S * pS;

   and if   pS= &S;

   then I can call function f in two ways:

        (*(pS->f))();
   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?

It shouldn't work AFAIK.  However, the following method should:
   pS->f();
You can call a function that you have a pointer to without needing to
dereference it.
--
"It takes a certain amount of shamelessness
 to be a monomaniac billionaire dwarf."
--Jon Katz <URL:http://slashdot.org/articles/99/03/17/1634238.shtml>
--



Mon, 03 Sep 2001 03:00:00 GMT  
 Pointers to Structure that contains pointer to Function
Thanks for the comment.  You are right, it should NOT
work.  This is an embedded compiler for an 8051. When
I tried compiling in two other compilers, including VC++,
and it gave an error that pS.f(); could not have a pointer
to the left of the dot.
James G

Quote:

>snip
>   then I can call function f in two ways:

> (*(pS->f))();
>   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?

>It shouldn't work AFAIK.  However, the following method should:
>   pS->f();
>You can call a function that you have a pointer to without needing to
>dereference it.
>--
>"It takes a certain amount of shamelessness
> to be a monomaniac billionaire dwarf."
>--Jon Katz <URL:http://slashdot.org/articles/99/03/17/1634238.shtml>
>--


--



Thu, 06 Sep 2001 03:00:00 GMT  
 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

--



Fri, 14 Sep 2001 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. dereferencing pointer to a structure containing a pointer to int

2. memory block containing pointers, aka pointer to pointer i believe

3. How to use (pointer to function), and function and pointer to (pointer to function)

4. Assigning structure pointer to function pointer

5. Pointer Functions and Pointers to Pointer Functions

6. reading a structure containing pointers

7. structure containing pointer to char over the network

8. How Do I fwrite() and fread() A Structure that contains pointers to dynamic data

9. self contained pointers to structures

10. Structure containing pointer problem

11. Dispatch tables with functions containing managed pointers

12. Struct containing pointer to function

 

 
Powered by phpBB® Forum Software