Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Passing function pointers as arrays of chars

Reply
Thread Tools

Passing function pointers as arrays of chars

 
 
Olaf Dietrich
Guest
Posts: n/a
 
      10-22-2008
I may just be temporarily (hopefully ...) stupid, but how can
I pass a function pointer between functions using an array of
(signed/unsigned) chars (in a standard-conforming way)?


E.g.:

I have a function: int (*func)(double, int).

Now I'd like to store the "address" of this function
in an array (of sufficient size) of chars, e.g. in
char func_addr[100]. (How can I do this?)

And finally I would like to use this address in another
function pointer:
new_func = (int (*)(double, int)) func_addr.

This doesn't work - gcc complains: "ISO C forbids conversion
of object pointer to function pointer type".


Is there a way to store the data that constitutes the function
pointer in an array of chars (that need not have the size of
an object pointer)? And then to re-interpret this sequence of
bytes again as a function pointer?


Thanks for any suggestions
Olaf
 
Reply With Quote
 
 
 
 
James Kuyper
Guest
Posts: n/a
 
      10-22-2008
Olaf Dietrich wrote:
> I may just be temporarily (hopefully ...) stupid, but how can
> I pass a function pointer between functions using an array of
> (signed/unsigned) chars (in a standard-conforming way)?
>
>
> E.g.:
>
> I have a function: int (*func)(double, int).
>
> Now I'd like to store the "address" of this function
> in an array (of sufficient size) of chars, e.g. in
> char func_addr[100]. (How can I do this?)



char func_addr[sizeof func];
memcpy(func_addr, &func, sizeof func);

> And finally I would like to use this address in another
> function pointer:
> new_func = (int (*)(double, int)) func_addr.


memcpy(&new_func, func_addr, sizeof new_func);

> This doesn't work - gcc complains: "ISO C forbids conversion
> of object pointer to function pointer type".


That's because you're trying to convert func_addr, which in this context
decays into a pointer to the first char of that array, into a pointer to
a function. What you want it to do is interpret the contents of that
array as if it were a pointer to a function. The syntax for such a
conversion (were it safe) would be:

new_func = *(int (**)(double, int)) func_addr;

This converts func_addr from a pointer to an array of char into a
pointer to a function pointer, then dereferences that pointer, obtaining
a function pointer. This is not, unfortunately, a safe approach to
use, because there's no guarantee that func_addr is correctly aligned to
hold such a pointer. You could guarantee correct alignment if func_addr
were a pointer to a dynamically allocated array of char, or if func_addr
were part of a union with a the appropriate function pointer type.
 
Reply With Quote
 
 
 
 
Olaf Dietrich
Guest
Posts: n/a
 
      10-22-2008
James Kuyper <(E-Mail Removed)>:
> Olaf Dietrich wrote:
>
>> Now I'd like to store the "address" of this function
>> in an array (of sufficient size) of chars, e.g. in
>> char func_addr[100]. (How can I do this?)

>
> char func_addr[sizeof func];
> memcpy(func_addr, &func, sizeof func);
>
>> And finally I would like to use this address in another
>> function pointer:

>
> memcpy(&new_func, func_addr, sizeof new_func);


How obvious and simple ... (and it even works).

Thank you very much for your help
Olaf
 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      10-22-2008
On 22 Oct, 13:10, (E-Mail Removed) (Olaf Dietrich) wrote:

> I may just be temporarily (hopefully ...) stupid, but how can
> I pass a function pointer between functions using an array of
> (signed/unsigned) chars (in a standard-conforming way)?


why do you want to do this? Some sort of callback function?
It's best avoided if you can.

<snip>

--
Nick Keighley


 
Reply With Quote
 
vippstar@gmail.com
Guest
Posts: n/a
 
      10-22-2008
On Oct 22, 3:31 pm, James Kuyper <(E-Mail Removed)> wrote:
> Olaf Dietrich wrote:
> > I may just be temporarily (hopefully ...) stupid, but how can
> > I pass a function pointer between functions using an array of
> > (signed/unsigned) chars (in a standard-conforming way)?

>
> > E.g.:

>
> > I have a function: int (*func)(double, int).

>
> > Now I'd like to store the "address" of this function
> > in an array (of sufficient size) of chars, e.g. in
> > char func_addr[100]. (How can I do this?)

>
> char func_addr[sizeof func];
> memcpy(func_addr, &func, sizeof func);


How will this work since the conversion between void * and a function
pointer needs not to be meaningful? (6.3.2.3)
(notice the memcpy call)
Also, while the first line is possible for function pointers, it's not
for function designators;
This is correct:

void (*fp)(void);
char foo[sizeof fp];

This is incorrect:

char foo[sizeof strcpy];

Because it violates the constraint in 6.5.3.4
 
Reply With Quote
 
jameskuyper@verizon.net
Guest
Posts: n/a
 
      10-22-2008
(E-Mail Removed) wrote:
> On Oct 22, 3:31 pm, James Kuyper <(E-Mail Removed)> wrote:
> > Olaf Dietrich wrote:

....
> > > I have a function: int (*func)(double, int).

> >
> > > Now I'd like to store the "address" of this function
> > > in an array (of sufficient size) of chars, e.g. in
> > > char func_addr[100]. (How can I do this?)

> >
> > char func_addr[sizeof func];
> > memcpy(func_addr, &func, sizeof func);

>
> How will this work since the conversion between void * and a function
> pointer needs not to be meaningful? (6.3.2.3)
> (notice the memcpy call)


No such conversion is performed by this code. The only pointers being
converted are func_addr and &func, in the memcpy() call that you refer
to. Both of those are pointers to objects. func_addr is pointer to a
char object, and &func is a pointer to a function pointer object.
Neither one is a pointer to a function. func itself is a pointer to a
function, but func is NOT being converted to void*.

> Also, while the first line is possible for function pointers, it's not
> for function designators;


The question I answered was specifically about func, which is a
function pointer object. Function pointer values have to be stored in
a function pointer object before this approach can be used.
 
Reply With Quote
 
vippstar@gmail.com
Guest
Posts: n/a
 
      10-22-2008
On Oct 22, 8:50 pm, (E-Mail Removed) wrote:
> (E-Mail Removed) wrote:
> > On Oct 22, 3:31 pm, James Kuyper <(E-Mail Removed)> wrote:
> > > Olaf Dietrich wrote:

> ...
> > > > I have a function: int (*func)(double, int).

>
> > > > Now I'd like to store the "address" of this function
> > > > in an array (of sufficient size) of chars, e.g. in
> > > > char func_addr[100]. (How can I do this?)

>
> > > char func_addr[sizeof func];
> > > memcpy(func_addr, &func, sizeof func);

>
> > How will this work since the conversion between void * and a function
> > pointer needs not to be meaningful? (6.3.2.3)
> > (notice the memcpy call)

>
> No such conversion is performed by this code. The only pointers being
> converted are func_addr and &func, in the memcpy() call that you refer
> to. Both of those are pointers to objects. func_addr is pointer to a
> char object, and &func is a pointer to a function pointer object.
> Neither one is a pointer to a function. func itself is a pointer to a
> function, but func is NOT being converted to void*.


Could you give me the type of func?
You say it's a pointer to a function pointer.
Does it mean it's T (**)()?
If so, then your code works and I apologise, however... (read below)

> > Also, while the first line is possible for function pointers, it's not
> > for function designators;

>
> The question I answered was specifically about func, which is a
> function pointer object. Function pointer values have to be stored in
> a function pointer object before this approach can be used.


Here you say func is a function pointer object, which contradicts what
you said before.

After some test, I realize where I was wrong:

& doesn't have an effect with function designators, however it does
have for function pointers.
Therefore, your code is correct and I was wrong.
 
Reply With Quote
 
jameskuyper@verizon.net
Guest
Posts: n/a
 
      10-22-2008
(E-Mail Removed) wrote:
> On Oct 22, 8:50 pm, (E-Mail Removed) wrote:
> > (E-Mail Removed) wrote:
> > > On Oct 22, 3:31 pm, James Kuyper <(E-Mail Removed)> wrote:
> > > > Olaf Dietrich wrote:

> > ...
> > > > > I have a function: int (*func)(double, int).

....
> > ... and &func is a pointer to a function pointer object.

....
> Could you give me the type of func?


See above. It's the first line of the message you were responding to.

> You say it's a pointer to a function pointer.


No, I said that &func is a pointer to a function pointer. func itself
is the function pointer that &func points at.

> Does it mean it's T (**)()?


No, &func has type int (**)(double,int).

....
> > The question I answered was specifically about func, which is a
> > function pointer object.

....
> Here you say func is a function pointer object, which contradicts what
> you said before.


The first statement was about &func, the second statement was about
func.

> & doesn't have an effect with function designators, however it does
> have for function pointers.
> Therefore, your code is correct and I was wrong.


Well, that would explain the confusion.
 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      10-22-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:


> > > > > I have a function: int (*func)(double, int).


> Could you give me the type of func?


You already had it.



Brian
 
Reply With Quote
 
vippstar@gmail.com
Guest
Posts: n/a
 
      10-22-2008
On Oct 22, 10:44 pm, "Default User" <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
> > > > > > I have a function: int (*func)(double, int).

> > Could you give me the type of func?

>
> You already had it.


Yes, but afterwards I explained in my post my confusion.
I have the habbit of writing my message as I read the other article,
which I think makes my messages somewhat unclear, so I'm trying to
stop doing it.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointers, pointers, pointers... cerr C Programming 12 04-07-2011 11:17 PM
How to truncate char string fromt beginning and replace chars instring by other chars in C or C++? Hongyu C++ 9 08-08-2008 12:18 PM
Help structuring my program (arrays of function pointers/ passing variables to functions) googlinggoogler@hotmail.com C Programming 3 07-20-2007 01:25 PM
Floats to chars and chars to floats Kosio C Programming 44 09-23-2005 09:49 AM
receiving ??? chars instead of "special" chars M.Posseth ASP .Net Web Services 3 11-16-2004 07:00 PM



Advertisments