Solved

Direct Function Calling

Posted on 1997-02-22
1
175 Views
Last Modified: 2010-04-10
I need to be able to call a function given only it's address.  I want to be able to pass that address into one function and then call the function that address coresponds to.

void foo(int i){
  //Some Code Here
}
void foo2(int i){
  //Some Code Here
}

void caller(void *tmp){
  void *ptr = tmp; //Make ptr = address of foo
  ptr(1);
  }

void main(void){
  caller(foo);
  caller(foo2);
}

I realize this does not work but I want to do something similar to this.  Is there an easy way to do it or do I need to use assembly?
I known I could use classes and virtual functions but would like to do it this way.

Thanx
lordx@wam.umd.edu
0
Comment
Question by:lordx
1 Comment
 
LVL 3

Accepted Solution

by:
os012897 earned 70 total points
ID: 1162110
As far as I can remember it should work like that:

.....
...
.

void caller (void *tmp)

{    (*tmp) (1);   //dereference function + call it with argument
}

void main (void)

{    caller (&foo);  //get address of foo
     caller (&foo2); //get address of foo2
}


Regards,

       os

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now