473,406 Members | 2,549 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,406 software developers and data experts.

Getting address of a function through some string

#include<string.h>

void (*foobar)();
void main()
{

char *str;
strcpy(str,"Fun1");
strcat(str,"method1");

foobar= ( Here i should get the address of Fun1method1 function through "str")

(*foobar)();
}

void Fun1Method1()
{
printf("Here ... I am !!!");
}
Can any one please help with this ....
Nov 14 '05 #1
7 1799

Prashanth Badabagni wrote:
#include<string.h>

void (*foobar)();
void main()
{

char *str;
strcpy(str,"Fun1");
strcat(str,"method1");

foobar= ( Here i should get the address of Fun1method1 function through "str")
Its not possible that way !

Instead, what you want is:

foobar = Fun1Method1;

Also, if you want to print out the address of that function you
can use
printf ("Address of Fun1Method1 = %p\n", (void *)Fun1Method1 );
(*foobar)();
}

void Fun1Method1()
{
printf("Here ... I am !!!");
}
Can any one please help with this ....


Nov 14 '05 #2

"Prashanth Badabagni" <b_*********@hotmail.com> wrote in message
news:d1**************************@posting.google.c om...
#include<string.h>

void (*foobar)();
void main()
{

char *str;
strcpy(str,"Fun1");
strcat(str,"method1");

foobar= ( Here i should get the address of Fun1method1 function through "str")
(*foobar)();
}

void Fun1Method1()
{
printf("Here ... I am !!!");
}
Can any one please help with this ....


#include <stdlib.h>
#include <string.h>

typedef void (*fnc_ptr_t)(void);
typedef struct fnc_entry_t
{
const char* name;
fnc_ptr_t fnc;
}fnc_entry_t;

fnc_entry_t fnc_table[] =
{
{ "Foo", foo },
{ "Bar", bar },
{ "Bla", bla },
{ NULL, NULL }
}

fnc_ptr_t
fnc_by_name(const char* name)
{
int i;
for(i=0; NULL != fnc_table[i].name && 0 != strcmp(name. fnc_table[i].name)
; i++)
{
/* nothing */
}

return fnc_table[i].fnc;
}

Nov 14 '05 #3
Prashanth Badabagni wrote:

#include<string.h>

void (*foobar)();
void main()
{

char *str;
strcpy(str,"Fun1");
strcat(str,"method1");

foobar= ( Here i should get the address of Fun1method1 function through "str")

(*foobar)();
}

void Fun1Method1()
{
printf("Here ... I am !!!");
}
Can any one please help with this ....


Your code has a number of problems.

Firstly, main returns int, not void, so change the return type, and
add return 0; at the end of main.

Secondly, you're missing a prototype for printf, so add <stdio.h>
to your list of includes.

Thirdly, you reserve no storage for the target of your strcpy and
strcat.

Fourthly, you'll need a prototype for Fun1Method1().

Fifthly, I think you meant to strcat "Method1", not "method1".

Sixthly, it's a good idea to add a \n to your printf, to make
sure the output appears when you expect it to.

Let's solve those problems, and answer your question at the same
time.

#include <stdio.h>
#include <string.h>

void (*foobar)();
void Fun1Method1(void);

int main(void)
{
char str[12]; /* big enough for "Fun1Method1" */

strcpy(str, "Fun1");
strcat(str, "Method1");

if(strcmp(str, "Fun1Method1") == 0)
{
foobar = Fun1Method1;
(*foobar)();
}

return 0;
}

void Fun1Method1(void)
{
printf("Here ... I am !!!\n");
}
If you have many such functions, you may wish to consider
constructing a lookup table, comprised of structs containing
a string with the function name, and a pointer to the
appropriate function:

struct VOID_FUNCTION_VOID_LOOKUP
{
char Name[32]; /* based on the maximum unique identifier length
* guaranteed by ISO C90.
*/
void (*Function)(void);
};

Construct an array of these structures, populate the array,
and sort it by name (see qsort). You can then use bsearch
to find the right entry in the array, and dereference the
pointer you get (if it isn't NULL) to get a pointer to the
appropriate struct. Then it's just a matter of accessing
the Function member of the struct.
Nov 14 '05 #4
Ravi Uday wrote:

Prashanth Badabagni wrote:

foobar= ( Here i should get the address of Fun1method1 function through "str")
Its not possible that way !


True, and the code had other problems too.
Instead, what you want is:

foobar = Fun1Method1;
Before he can do that, he'll need a declaration of Fun1Method1.

Also, if you want to print out the address of that function you
can use
printf ("Address of Fun1Method1 = %p\n", (void *)Fun1Method1 );


No, you can't. Function pointers are not guaranteed to be
convertible to void *.
Nov 14 '05 #5
On 12 Jan 2005 23:57:54 -0800, in comp.lang.c , b_*********@hotmail.com
(Prashanth Badabagni) wrote:

(a code example but no description of what he was trying to do).

At a rough guess, you want 20.6 in the FAQ. But next time, please post a
clear statement of your question or problem, not just some code.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 14 '05 #6
Mark McIntyre <ma**********@spamcop.net> wrote in message news:<eo********************************@4ax.com>. ..
On 12 Jan 2005 23:57:54 -0800, in comp.lang.c , b_*********@hotmail.com
(Prashanth Badabagni) wrote:

(a code example but no description of what he was trying to do).

At a rough guess, you want 20.6 in the FAQ. But next time, please post a
clear statement of your question or problem, not just some code.


Mark,

What i am supposed to do was ... I want to assign the address of
some function ( whose name is not known to me ) to a function
pointer.I would provide some standard format for the name of the
function.The users must define the function with the defined format
before they use my library ( All the above code is embedded into a
library).I found this idea very much useful with the menu
implementation.When a user goes through a multi level menu and hits
enter at some menu item.The corresponding function will get assigned
at runtime to a function pointer and it will be called ultimately.For
this to happen i have to assign the function address with out knowing
it's name .
Nov 14 '05 #7
On 12 Jan 2005 23:57:54 -0800, b_*********@hotmail.com (Prashanth
Badabagni) wrote:
#include<string.h>

void (*foobar)();
void main()
{

char *str;
strcpy(str,"Fun1");
strcat(str,"method1");

foobar= ( Here i should get the address of Fun1method1 function through "str")

(*foobar)();
}


<snip>

See the clc FAQ, question 20.6:

20.6: If I have a char * variable pointing to the name of a function,
how can I call that function?

http://www.eskimo.com/~scs/C-faq/q20.6.html
Following is an implementation of this idea:

#include <stdio.h>
#include <stdlib.h>

int fcn1(int n) { return n; }
int fcn2(int n) { return n*2; }
int fcn3(int n) { return n*3; }

typedef struct
{
int (*fcn_p)(int n); // Pointer to function "int foo(int)"
const char *fcn_name; // Name of function as string
}
fpa_T;
#define NUM_FCNS 3

int main(void)
{
int i, num = 12;
fpa_T fpa[NUM_FCNS] =
{
{ fcn1, "fcn1" }, // Assign fcn pointers & strings here
{ fcn2, "fcn2" },
{ fcn3, "fcn3" }
};

for (i = 0; i < NUM_FCNS; i++)
printf("%s(%d) = %d\n", fpa[i].fcn_name, num, fpa[i].fcn_p(num));

return EXIT_SUCCESS;
}
--
Robert B. Clark (email ROT13'ed)
Visit ClarkWehyr Enterprises On-Line at http://www.3clarks.com/ClarkWehyr/
Nov 14 '05 #8

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

Similar topics

7
by: Nolan Martin | last post by:
is a static functions address constant? ie.. static void func(); write_to_file(&func); Restart program... static void func(); void (*funcPtr) ();
8
by: Timo | last post by:
I am trying to get address of myStruct to a string array texts. I am using M$ Visual C++ 6.0 (this is not OS specific question, though, this code should also work on 16 bit embedded compiler ;)). ...
17
by: Olivier Bellemare | last post by:
I've tried to make a function that returns the middle of a string. For example: strmid("this is a text",6,4); would return "is a". Here is my code: char *strmid(char *texte, int depart,...
2
by: Alex | last post by:
Hi all, I'm writing a small web application which searches a database based on a date field, and populates a datagrid control with the results. The datagrid control has selection buttons added...
2
by: pangel83 | last post by:
I've been trying for days to write a piece of VB.NET code that will read from winamp's memory space the paths of the files from the current winamp playlist. The GETPLAYLISTFILE command of the...
1
by: Lyle Fairfield | last post by:
Option Explicit ' requires VBScript to be installed ' (maybe don't give this to your sugnificant other as ' it gets deleted addresses as well as current) ' obvious fixups needed '1. how get...
19
by: Manish Tomar | last post by:
Hi All, The following code as per my knowledge should not work: int* some() { int b = 10; return &b; }
11
by: Jan | last post by:
I'd like a maketable query listing ClientID, and E-mail from another table. This is simple, BUT I'd only like to get only the part after @ in the E-mail. How would this work in MS Access 2003?
7
vikas251074
by: vikas251074 | last post by:
I am getting error above in following code since few days giving tension day and night. How can I solve this? I am facing since Oct.25. in line no. 362 After doing a lot of homework, I am...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...
0
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...
0
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...

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.