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

How to make function XYZ able to call ANY arbitrary function ABC?

XYZ would have TWO input parameters:
a pointer to the function ABC (int * _p_abc)(...);
and a one-dimensional array of arguments to invoke ABC
(int * _abc_arguments)
The XYZ would look like:
void xyz( *_p_abc, *_abc_arguments) {}

The trick is in making XYZ able to call ABC with ANY number
of input arguments.

The problem is in this.
Say, for ABC with TWO arguments like int ABC(int, int), the
call would be (int * _p_abc) (2, 3);

For THREE arguments int ABC(int,int,int) the call would be
(int * _abc)(3,4,5).

How do I make XYZ, again, to call ABC with ANY number of arguments?

One very clumsy way to do that would be to use simple SWITCH:

switch:

case 1: (int * _p_abc) (x1);
case 2: (int * _p_abc) (x1,x2);
...
case 5: (int * _p_abc) (x1,x2,x3,x4,x5);

But it's TOO clumsy. I need a more elegant way to do that.
And, of course, the above is a simplified version in that ALL
arguments are integers. I will have to make it ultimately versatile so that it would take arguments of ANY type.
Jul 19 '15 #1
3 1616
weaknessforcats
9,208 Expert Mod 8TB
You could imitate what printf does.

The first argument is a char* which points to your format string. printf uses a % followed by an indicator that says what the type is. Like d means int and f means float.

You parse the string and the number of % is the number of arguments in the function you will call.

So, %d,%d,%f would let you call a function:

Expand|Select|Wrap|Line Numbers
  1. void ABC(int, int, float);
You need a function pointer to the function you call. Maybe the user passes in a void* as an argument which could then be typecast into a function pointer that takes int, int, float and returns void:

Expand|Select|Wrap|Line Numbers
  1. (*fp)(int, int, float)
making the call in this example:

Expand|Select|Wrap|Line Numbers
  1. (*fp)(int, int, float)(argint1, argint2, argfloat1);
You will need a case for all forms you are allowed to call. I know you are supposed to call using any number of function arguments but start with a reasonable number of cases to prove the concept.
Jul 20 '15 #2
donbock
2,426 Expert 2GB
What about the *_abc_arguments tells you how many arguments were passed?
What is the smallest number of arguments that can be passed?

You can declare the *_p_abc argument as a pointer to a function that takes "..." arguments, but you couldn't call xyz passing a function that takes a fixed number of arguments unless you cast the function pointer in the xyz call.

By the way, you shouldn't define any variables (including formal arguments) with leading underscore - those are reserved names for future versions of the C Standard.
Jul 20 '15 #3
donbock
2,426 Expert 2GB
This looks like an attempt to mimic object-oriented programming in C. I would assume you achieve your ultimate goal more easily with C++ (@weaknessforcats can help you with that).

whether you can elegantly achieve your goal in C depends on how flexible xyz has to be with the function pointer argument. If there are only a few possible function then the stdarg approach suggested above can work; but if xyz has to support an arbitrary or unpredictable number of unique function pointer argument values then I can't think of a way to avoid lots of casts or the argument switch that you show in the OP.
Jul 21 '15 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: FAN | last post by:
I want to define some function in python script dynamicly and call them later, but I get some problem. I have tried the following: ################################## # code...
38
by: Lasse Vågsæther Karlsen | last post by:
After working through a fair number of the challenges at www.mathschallenge.net, I noticed that some long-running functions can be helped *a lot* by caching their function results and retrieving...
3
by: news | last post by:
I have a document that can contain any number of iframes which have further copies of the same document (and so on). In practice, we shouldn't ever have frames within frames, but I'd like to make...
11
by: yangsuli | last post by:
i want to creat a link when somebody click the link the php script calls a function,then display itself :) i have tried <a href=<? funtion(); echo=$_server ?>text</a> but it will call the...
28
by: Larax | last post by:
Best explanation of my question will be an example, look below at this simple function: function SetEventHandler(element) { // some operations on element element.onclick = function(event) {
15
by: dspfun | last post by:
Hi, Is it possible to print the function name of the calling function? For example, f1() and f2() both calls f3(), in f3() I would like to print the name of the function calling f3() which...
28
by: ravi | last post by:
Hello everybody, I am writing a small application which does some work before the user main function starts execution. I am trying to #define the main function. But the problem is that,
12
by: aaragon | last post by:
I have this scenario: several arrays for which I have their fixed values at compilation time. Now, at runtime I need to access a specific array depending on an integer but I want to avoid if and...
6
by: RandomElle | last post by:
Hi there I'm hoping someone can help me out with the use of the Eval function. I am using Access2003 under WinXP Pro. I can successfully use the Eval function and get it to call any function with...
7
by: -Lost | last post by:
Is it possible to end the outermost function from an inner function? I've been tasked with implementing a feature check in some existing code and I wondered if this could be done: function...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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.