Solved

Direct Function Calling

Posted on 1997-02-22
1
187 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

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

831 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