473,545 Members | 1,876 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

cast to pointer to function

Hello,
I am looking for help for a cast problem:
I want to run functions in thread, using the pthread_create function.
The function to run is the first argument of pthread_create and is
expected to be of type void* (*func)(void*)
My problem is that I want to get the name of the function to run in
a .txt file that I parse at runtime.
I read the file and I get the name in a char* variable.
Now I want that this char* variable is the name of the function to
run. How do I do that?
I want to do something like:
char *func_to_run = scanf(the, good, args);
pthread_create (thread, attr, (magic cast)func_to_ru n, arg);

Even better, is it possible to test, at run time, that the provided
name is the name of a function that exists somewhere in the compiled
code?

Thanks for any help

Etienne

Jul 18 '07 #1
6 2862
On Wednesday 18 Jul 2007 3:25 pm, in comp.lang.c, etienne
<et***********@ lip6.frwrote:
Message ID: <11************ **********@g12g 2000prg.googleg roups.com>
Hello,
I am looking for help for a cast problem:
I want to run functions in thread, using the pthread_create
function. The function to run is the first argument of
pthread_create and is expected to be of type void* (*func)(void*)
My problem is that I want to get the name of the function to run in
a .txt file that I parse at runtime.
I read the file and I get the name in a char* variable.
Now I want that this char* variable is the name of the function to
run. How do I do that?
I want to do something like:
char *func_to_run = scanf(the, good, args);
pthread_create (thread, attr, (magic cast)func_to_ru n, arg);

Even better, is it possible to test, at run time, that the provided
name is the name of a function that exists somewhere in the
compiled code?

Thanks for any help
The group has discussed your problem many times before, AFAICT, the
consensus is that this cannot be done within Standard C code. Or
maybe it *can* be done, but then you might have to write an awful
lot of code to accomplish it.

Someone much more experienced than me will probably explain shortly.
Jul 18 '07 #2
etienne <et***********@ lip6.frwrote:
I am looking for help for a cast problem:
I want to run functions in thread, using the pthread_create function.
The function to run is the first argument of pthread_create and is
expected to be of type void* (*func)(void*)
My problem is that I want to get the name of the function to run in
a .txt file that I parse at runtime.
I read the file and I get the name in a char* variable.
Now I want that this char* variable is the name of the function to
run. How do I do that?
I want to do something like:
char *func_to_run = scanf(the, good, args);
pthread_create (thread, attr, (magic cast)func_to_ru n, arg);
Unfortunately, there's no "magic cast" that can convert a
string into a pointer to a function of that name, simply
because information about function and variable names is
not stored in the program - functions and variables get
converted to addresses during compilation (or at least
linking).
Even better, is it possible to test, at run time, that the provided
name is the name of a function that exists somewhere in the compiled
code?
If you don't insist on a "magic cast" there's a trival solution.
Just create an array of structures of a type like this:

struct name2address {
const char *name;
void * ( * addr ) ( void * );
};

Make the array large enough to hold as many structures as you
have functions that you want to know by both name and address
and then initialize the array accordingly. Now, when you read
in your file you just have to search this array for the element
where the name field is identical to the name of the function
you read from the text file. This then gives you the address of
function which you then can use to call it. And a failure to
find the function in the array of structures immediately tells
you that what you did read from the text file was invalid.

Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\______________ ____________ http://toerring.de
Jul 18 '07 #3
jt@toerring.de (Jens Thoms Toerring) wrote:
# etienne <et***********@ lip6.frwrote:
# I am looking for help for a cast problem:
# I want to run functions in thread, using the pthread_create function.
# The function to run is the first argument of pthread_create and is
# expected to be of type void* (*func)(void*)
# My problem is that I want to get the name of the function to run in
# a .txt file that I parse at runtime.
# I read the file and I get the name in a char* variable.
# Now I want that this char* variable is the name of the function to
# run. How do I do that?
# I want to do something like:
# char *func_to_run = scanf(the, good, args);
# pthread_create (thread, attr, (magic cast)func_to_ru n, arg);
#
# Unfortunately, there's no "magic cast" that can convert a
# string into a pointer to a function of that name, simply
# because information about function and variable names is

There is no magic cast in ANSI C, but there may be system
specific ways to get this. For example, you may be able to
get a load map from the loader and postprocess this into
a function name map; but this would specific to your loader
and development system.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
If you plan to shoplift, let us know.
Thanks
Jul 18 '07 #4
etienne wrote:
Hello,
I am looking for help for a cast problem:
I want to run functions in thread, using the pthread_create function.
The function to run is the first argument of pthread_create and is
expected to be of type void* (*func)(void*)
My problem is that I want to get the name of the function to run in
a .txt file that I parse at runtime.
I read the file and I get the name in a char* variable.
Now I want that this char* variable is the name of the function to
run. How do I do that?
I want to do something like:
char *func_to_run = scanf(the, good, args);
pthread_create (thread, attr, (magic cast)func_to_ru n, arg);

Even better, is it possible to test, at run time, that the provided
name is the name of a function that exists somewhere in the compiled
code?
This is Question 20.6 in the comp.lang.c Frequently
Asked Questions (FAQ) list, <http://c-faq.com/>.

--
Eric Sosman
es*****@ieee-dot-org.invalid
Jul 18 '07 #5
etienne wrote:
I want to do something like:
char *func_to_run = scanf(the, good, args);
pthread_create (thread, attr, (magic cast)func_to_ru n, arg);

Even better, is it possible to test, at run time, that the provided
name is the name of a function that exists somewhere in the compiled
code?
System specific solutions exist to call the dynamic linking loader:
You might want to look at the man page of functions
dlopen(), dlsym(), dlclose()...

--
regis
Jul 18 '07 #6
On 18 juil, 15:46, regis <regis.barbanc. ..@free.frwrote :
etienne wrote:
I want to do something like:
char *func_to_run = scanf(the, good, args);
pthread_create (thread, attr, (magic cast)func_to_ru n, arg);
Even better, is it possible to test, at run time, that the provided
name is the name of a function that exists somewhere in the compiled
code?

System specific solutions exist to call the dynamic linking loader:
You might want to look at the man page of functions
dlopen(), dlsym(), dlclose()...

--
regis

These functions do more or less what I want to do. I will try to use
them.
Thanks for your help.

Jul 19 '07 #7

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

Similar topics

18
5929
by: Graham Nicholls | last post by:
Hi. I'm having some fun with numbers. I've extraced an image sizes from a jpeg file img_x,img_y=image.getsize() then I'm trying to use those sizes to scale the image, but because python has decided that they are integers, I keep getting division by zero errors
5
2011
by: Patrick Guio | last post by:
Dear all, I have a C function that returns a void pointer (void *). This object pointer is actually a pointer to function. I have the following declarations: void *pobj; double (*func)(double);
10
2592
by: LaEisem | last post by:
On-the-job, I have "inherited" a lot of old C language software. A question or two about when "casting" of null pointer constants is needed has occurred during behind-the-scenes cleanup of some of that software. That subject seems not to be addressed, at least not directly, in the C FAQ where FAQ 5.2 seems most relevant. References: * C...
9
2703
by: Frederick Gotham | last post by:
Let's assume that we're working on the following system: CHAR_BIT == 8 sizeof( char* ) == 4 (i.e. 32-Bit) Furthermore, lets assume that the memory addresses are distributed as follows: 0x00000000 through 0xFFFFFFFE : Valid byte addresses
7
5249
by: llothar | last post by:
When i use -W4 on visual c 7.0 i get warning C4054 translator1.c(1703) : warning C4054: 'type cast' : from function pointer 'void * (__cdecl *)(se_agent *)' to data pointer 'void *' translator1.c(1703) : warning C4152: nonstandard extension, function/ data pointer conversion in expression whenever i cast a function pointer to a void* or...
18
2187
by: Felix Kater | last post by:
I haven't been thinking about it for years but recently I've stumbled on the fact that 'casting' is actually doing (at least) two different things: On the one hand 'casting' means: 'Change something into somthing else'. Example: double d=9.99; long l=(double)d; On the other hand 'casting' means: 'Treating something as something else...
1
452
by: etienne | last post by:
Hello all, i'm experimenting a problem in trying to cast a char * variable to a pointer to a function. My problem is that I want to execute functions in threads, using the pthread_create function. The name of the functions are read in txt file at runtime. So I want to do something like char * func_to_run = scanf(the, good, args);...
5
10761
by: =?utf-8?b?QXNiasO4cm4gU8OmYsO4?= | last post by:
For various reasons, I have a number of typedefs to name arrays of various sizes and for various purposes. I also have functions taking variables of such typedef-ed types as their input arguments. Now I want to feed one such function part of a larger array. To have the correct type in the call to the function, I am trying to use a cast....
3
2076
by: Bartc | last post by:
I have a variable like this: int *p; Sometimes this points to an int location containing the address of a void(void) function. How can I cast it so that I can call that function? I've tried casting to void **(void). The compiler keeps saying such a cast
7
3941
by: * Tong * | last post by:
Hi, I couldn't figure out how to properly type cast in this case: $ cat -n type_cast.c 1 #include <stdio.h> 2 3 typedef unsigned char Byte; 4 typedef signed char Small_Int; 5
0
7387
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7798
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7405
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7738
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5316
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4938
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3436
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1862
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
688
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.