Solved

Direct Function Calling

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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. …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

932 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

8 Experts available now in Live!

Get 1:1 Help Now