Assigning structure pointer to function pointer 
Author Message
 Assigning structure pointer to function pointer

I have a program in which I am assigning

1)      T (*fuptr)() = (struct X *) // function pointer = pointer to
structure.
2)      T (*fuptr)() = (double *)   // function pointer = pointer to double

Are these assignments portable? References to standard are welcome.
Moreover in the above situations is the casting necessary that is do I
have to do

        T (*fuptr) () = ( T (*)() )  ( & x );

Or at least is the casting *here* a good practice or not?

--
       {*filter*}in the air in the land of hypocricy
                -- RATM

maniac_king AT msn DOT com



Thu, 30 Jun 2005 03:05:33 GMT  
 Assigning structure pointer to function pointer


Quote:
> I have a program in which I am assigning

> 1) T (*fuptr)() = (struct X *) // function pointer = pointer to
> structure.
> 2) T (*fuptr)() = (double *)   // function pointer = pointer to double

> Are these assignments portable?

No.

Quote:
> References to standard are welcome.
> Moreover in the above situations is the casting necessary

I'm wondering what you hope to achieve with converting
a pointer to function to a pointer to an object type.

?that is do I

Quote:
> have to do

> T (*fuptr) () = ( T (*)() )  ( & x );

Not defined by the language.

Quote:

> Or at least is the casting *here* a good practice or not?

Casting should generally be avoided, unless you know
absolutely what you're doing, and why.

-Mike



Thu, 30 Jun 2005 05:53:25 GMT  
 Assigning structure pointer to function pointer

Quote:



> > I have a program in which I am assigning

> > 1) T (*fuptr)() = (struct X *) // function pointer = pointer to
> > structure.
> > 2) T (*fuptr)() = (double *)   // function pointer = pointer to double

> > Are these assignments portable?

> No.

Then my question is this, can void* be used when I have to use it to
store a pointer to function.

Quote:

> > References to standard are welcome.
> > Moreover in the above situations is the casting necessary

> I'm wondering what you hope to achieve with converting
> a pointer to function to a pointer to an object type.

I am trying to build up an interpreter based upon the design of hoc as
outlined in the UNIX programming language by bwk and rp.

In there

typedef int  (*Inst)(); // Pointer to function returning an int

and a

struct Symbol {
      ////

Quote:
};

Then an array of Inst is used to store the Symbol*.

One more question however can be portably assign any union and
structure pointer to any other union or structure pointer.

Quote:

> ?that is do I
> > have to do

> > T (*fuptr) () = ( T (*)() )  ( & x );

> Not defined by the language.

> > Or at least is the casting *here* a good practice or not?

> Casting should generally be avoided, unless you know
> absolutely what you're doing, and why.

> -Mike

Thanks for the reply.

--  
Minti



Fri, 01 Jul 2005 02:03:39 GMT  
 Assigning structure pointer to function pointer

Quote:





>> > I have a program in which I am assigning

>> > 1) T (*fuptr)() = (struct X *) // function pointer = pointer to
>> > structure.
>> > 2) T (*fuptr)() = (double *)   // function pointer = pointer to double

>> > Are these assignments portable?

>> No.

> Then my question is this, can void* be used when I have to use it to
> store a pointer to function.

Yes. void * can hold any type of pointer, provided you cast it properly.

- Show quoted text -

Quote:

>> > References to standard are welcome.
>> > Moreover in the above situations is the casting necessary

>> I'm wondering what you hope to achieve with converting
>> a pointer to function to a pointer to an object type.

> I am trying to build up an interpreter based upon the design of hoc as
> outlined in the UNIX programming language by bwk and rp.

> In there

> typedef int  (*Inst)(); // Pointer to function returning an int

> and a

> struct Symbol {
>       ////
> };

> Then an array of Inst is used to store the Symbol*.

Why? Why not an array of Symbol*? If you need both Inst and Symbol*, use a
union.

Quote:
> One more question however can be portably assign any union and
> structure pointer to any other union or structure pointer.

I don't understand.

--
Replace spamtrap with bd to reply.
She been married so many times she got rice marks all over her face.
                -- Tom Waits



Fri, 01 Jul 2005 07:38:13 GMT  
 Assigning structure pointer to function pointer

Quote:






>>> > I have a program in which I am assigning

>>> > 1) T (*fuptr)() = (struct X *) // function pointer = pointer to
>>> > structure.
>>> > 2) T (*fuptr)() = (double *)   // function pointer = pointer to double

>>> > Are these assignments portable?

>>> No.

>> Then my question is this, can void* be used when I have to use it to
>> store a pointer to function.

> Yes. void * can hold any type of pointer, provided you cast it properly.

Wrong.  void * can hold any pointer to an object, but a function is not
an object.

        - Kevin.



Fri, 01 Jul 2005 08:16:34 GMT  
 Assigning structure pointer to function pointer

Quote:






> >> > I have a program in which I am assigning

> >> > 1) T (*fuptr)() = (struct X *) // function pointer = pointer to
> >> > structure.
> >> > 2) T (*fuptr)() = (double *)   // function pointer = pointer to double

> >> > Are these assignments portable?

> >> No.

> > Then my question is this, can void* be used when I have to use it to
> > store a pointer to function.

> Yes. void * can hold any type of pointer, provided you cast it properly.

> >> > References to standard are welcome.
> >> > Moreover in the above situations is the casting necessary

> >> I'm wondering what you hope to achieve with converting
> >> a pointer to function to a pointer to an object type.

> > I am trying to build up an interpreter based upon the design of hoc as
> > outlined in the UNIX programming language by bwk and rp.

> > In there

> > typedef int  (*Inst)(); // Pointer to function returning an int

> > and a

> > struct Symbol {
> >       ////
> > };

> > Then an array of Inst is used to store the Symbol*.

> Why? Why not an array of Symbol*? If you need both Inst and Symbol*, use a
> union.

Thanks I will try that.

Quote:

> > One more question however can be portably assign any union and
> > structure pointer to any other union or structure pointer.

> I don't understand.

What I mean to ask is that given ANY structure/union pointer can I
PORTABLY assign it to any other structure/union pointer.

i.e.

(struct x*) = (struct y*)

P.S. --> The name of the book I mentioned is wrong it is UNIX
programming *ENVIRONMENT*

--
Minti
maniac_king AT msn DOT com



Sat, 02 Jul 2005 08:18:16 GMT  
 Assigning structure pointer to function pointer

Quote:

> What I mean to ask is that given ANY structure/union pointer can I
> PORTABLY assign it to any other structure/union pointer.

> i.e.

> (struct x*) = (struct y*)

That's not valid code.  But I think I know what you meant:

void xyzzy(struct y *p_y)
{
    struct x *p_x = (struct x *)p_y;

...yes, you can portably do this (with the cast), because pointers to
struct types are required to have the same representation.  However,
that's not to say that you can then go dereferencing p_x - there is some
justification for believing the if struct x and struct y share a common
initial sequence of members then you can portably access those through
either pointer type, but I don't think this is explicitly stated.
What exactly are you trying to do?

        - Kevin.



Sat, 02 Jul 2005 14:51:09 GMT  
 Assigning structure pointer to function pointer

...
Quote:
> What I mean to ask is that given ANY structure/union pointer can I
> PORTABLY assign it to any other structure/union pointer.

> i.e.

> (struct x*) = (struct y*)

Effectively, yes.

C99 requires that all struct pointers (that is, all pointer to
struct types) have the same representation and alignment
requirements, and similarly for all union pointers.  C90
did not require this explicitly, but did require that pointers
to similarly-declared structs in different t.u.s be compatible
and also with pointers to the incomplete (tag-only) form of
the struct, and similarly for unions; in practice this requires
the same representation.  The types pointer to struct x and
... y are not *compatible* though so a cast is required.
Technically this isn't required to work between a struct
and a union (or vice versa) but in practice it does.

--
- David.Thompson 1 now at worldnet.att.net



Thu, 07 Jul 2005 12:00:11 GMT  
 
 [ 8 post ] 

 Relevant Pages 

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

2. Pointers to Structure that contains pointer to Function

3. Pointer Functions and Pointers to Pointer Functions

4. Problem assigning function to function pointer

5. Problems assigning structure members to pointers

6. Pointers assigned to pointers - confused

7. Assigning "putchar" to a function pointer

8. Assigning "putchar" to a function pointer

9. Inherited class function calls from a pointer assigned to the base class

10. Question about signal()/pointers to functions that return pointers to functions

11. function pointers and function pointers array

12. C++ function pointers versus C function pointer problem

 

 
Powered by phpBB® Forum Software