473,385 Members | 1,732 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Passing function pointers as arrays of chars

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
Oct 22 '08 #1
10 5067
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.
Oct 22 '08 #2
James Kuyper <ja*********@verizon.net>:
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
Oct 22 '08 #3
On 22 Oct, 13:10, o...@dtrx.de (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
Oct 22 '08 #4
On Oct 22, 3:31 pm, James Kuyper <jameskuy...@verizon.netwrote:
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
Oct 22 '08 #5
vipps...@gmail.com wrote:
On Oct 22, 3:31 pm, James Kuyper <jameskuy...@verizon.netwrote:
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.
Oct 22 '08 #6
On Oct 22, 8:50 pm, jameskuy...@verizon.net wrote:
vipps...@gmail.com wrote:
On Oct 22, 3:31 pm, James Kuyper <jameskuy...@verizon.netwrote:
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. :-)
Oct 22 '08 #7
vipps...@gmail.com wrote:
On Oct 22, 8:50 pm, jameskuy...@verizon.net wrote:
vipps...@gmail.com wrote:
On Oct 22, 3:31 pm, James Kuyper <jameskuy...@verizon.netwrote:
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.
Oct 22 '08 #8
vi******@gmail.com wrote:

I have a function: int (*func)(double, int).
Could you give me the type of func?
You already had it.

Brian
Oct 22 '08 #9
On Oct 22, 10:44 pm, "Default User" <defaultuse...@yahoo.comwrote:
vipps...@gmail.com 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.
Oct 22 '08 #10
Nick Keighley <ni******************@hotmail.com>:
On 22 Oct, 13:10, o...@dtrx.de (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?
I was trying to pass a pointer to a C function (used for
non-linear numerical fitting) between Tcl commands. I can
easily pass arbitrary binary data as arrays of (unsigned)
chars using the available library "procedures" such as
Tcl_SetByteArrayObj(), but it's not possible to pass
function pointers directly (without defining any additional
new types of Tcl objects etc.)

It's best avoided if you can.
It's sort of quick and dirty, but it works (using
memcpy as suggested by James Kuyper).
Olaf
Oct 23 '08 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
2
by: Adam Balgach | last post by:
Greetings everyone, ive got a problem ive been working with for quite a while and need some help. ive got a structure: struct Data { char *data1; char *data2; int val1; int val2;
8
by: kalinga1234 | last post by:
there is a problem regarding passing array of characters to another function(without using structures,pointer etc,).can anybody help me to solve the problem.
2
by: Morgan | last post by:
Thanks to all of you because I solved the problem related with my previous post. I simply made confusion with pointers to pointers and then succeeded passing the reference to the first element...
4
by: Christian Maier | last post by:
Hi After surfing a while I have still trouble with this array thing. I have the following function and recive a Segmentation fault, how must I code this right?? Thanks Christian Maier
2
by: luis | last post by:
I'm using ctypes to call a fortran dll from python. I have no problems passing integer and double arryas, but I have an error with str arrys. For example: ..... StringVector = c_char_p *...
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.