Solved

Direct Function Calling

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
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.

830 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