You are not logged in.

#1 2013-03-22 03:13:52

BennyBolton
Member
From: Christchurch New Zealand
Registered: 2011-10-14
Posts: 63
Website

c call function without knowing parameters

I need to write a function that is given a function pointer, and a list of types (integers that represent a type). I can obtain the values to pass to the function pointer, but am wondering how to call it. This needs to happen at run time and i dont know how many arguments i will have. e.g.

void call_function(void (*func)(),int types[],void *args[]){
	int i;
	for(i=0;types[i]!=0;i++){
		// Add this argument
	}
	// call the function with the arguments
}

HP DV6 + Arch + Openbox

Offline

#2 2013-03-22 03:30:38

Trilby
Forum Moderator
From: Massachusetts, USA
Registered: 2011-11-29
Posts: 14,211
Website

Re: c call function without knowing parameters

Use va_args and make a variadic function.

Common examples can be seen in error functions that wrap a printf.


InterrobangSlider
• How's my coding? See this page.
• How's my moderating? Feel free to email any concerns, complaints, or objections.

Offline

#3 2013-03-22 03:34:17

BennyBolton
Member
From: Christchurch New Zealand
Registered: 2011-10-14
Posts: 63
Website

Re: c call function without knowing parameters

the function pointer doesn't take a function, and I don't think there is a way the create them. If there is a way to create them, how do I pass it to a function that doesn't take one?


HP DV6 + Arch + Openbox

Offline

#4 2013-03-22 04:26:30

Trilby
Forum Moderator
From: Massachusetts, USA
Registered: 2011-11-29
Posts: 14,211
Website

Re: c call function without knowing parameters

Huh?  Pronoun overflow error.

Why would the function pointer need to take a function?  What them are you trying to create?  What them became an it when being passed to what?

Last edited by Trilby (2013-03-22 05:14:53)


InterrobangSlider
• How's my coding? See this page.
• How's my moderating? Feel free to email any concerns, complaints, or objections.

Offline

#5 2013-03-22 04:40:52

progandy
Member
Registered: 2012-05-17
Posts: 2,151

Re: c call function without knowing parameters

Why would you do that? I can only think it could be useful in interpretet languages. You might want to look into dyncall, there are others but I don't remember the names.

Offline

#6 2013-03-22 05:02:30

MagicSkyFairy
Member
Registered: 2013-03-14
Posts: 79

Re: c call function without knowing parameters

Can you give more details of what you need to do?  Are you trying to figure out the function, or how to put the variables together and call the function?

It doesn't make much sense to pass a function pointer in C.  Are you sure that's a requirement?

This smells of a homework assignment, so I'll help, but I'm going to be just vague enough so you still have to figure out yourself. buahahaha, I just need a more specific description of what needs to be done.


I have wasted atleast a second of your time by making you read my signature.

Offline

#7 2013-03-22 05:07:24

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 13,436

Re: c call function without knowing parameters

MagicSkyFairy wrote:

It doesn't make much sense to pass a function pointer in C.  Are you sure that's a requirement?

Sure it does.  How else would you implement a callback function ??

Very useful in GUIs, I/O processing, error handling,


edit:  For example, look at this page talking about Gtk.  Look at the g_signal_connect function.
http://zetcode.com/tutorials/gtktutorial/gtkevents/

Last edited by ewaller (2013-03-22 05:14:58)


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
You assume people are rational and influenced by evidence.  You must not work with the public much. -- Trilby
----
How to Ask Questions the Smart Way

Offline

#8 2013-03-22 05:15:11

BennyBolton
Member
From: Christchurch New Zealand
Registered: 2011-10-14
Posts: 63
Website

Re: c call function without knowing parameters

progandy wrote:

Why would you do that? I can only think it could be useful in interpretet languages. You might want to look into dyncall, there are others but I don't remember the names.

Yeh its an interpreted language, dyncall looks like what i need. Ill look into it furthur

Trilby wrote:

Why would the function pointer need to take a function?

My bad thats meant to be va_list

MagicSkyFairy wrote:

This smells of a homework assignment

nah mostly for fun but it may be usefull down the line in other projects

Last edited by BennyBolton (2013-03-22 05:18:50)


HP DV6 + Arch + Openbox

Offline

#9 2013-03-22 05:24:07

Trilby
Forum Moderator
From: Massachusetts, USA
Registered: 2011-11-29
Posts: 14,211
Website

Re: c call function without knowing parameters

I just found passing a function pointer was the best way to do something interesting with my WM.

I had a function that selected and called a window tiling function.

But when I started adding multimonitor support, I needed a function that did a few housekeeping bits, then tiled each monitor.  It was ugly and repetative until I had the first function pass a pointer to the tiling funtion to the monitor function.  The monitor function loops through monitors and calls whichever function it was passed.

But back OT.  I may have misread the first time.  Are you writing just the calling function, or are you also writing the function that is being passed as a function pointer?  If you are only doing the former, you cannot do this.  You must call a function with the correct number of arguments - unless it was declared as a variadic function.  If you are writing that function to - then see va_args, and you can do this.

In either case - this may be a good place for some inline assembly.  May being the operative word there - I do a good bit of C, and have dabbled in assembly, but I've never really combined the two.  But as I understand it, you should be able to push your arguments to the stack, then call the function.  This is how arguments are passed afterall.


InterrobangSlider
• How's my coding? See this page.
• How's my moderating? Feel free to email any concerns, complaints, or objections.

Offline

#10 2013-03-22 05:29:29

BennyBolton
Member
From: Christchurch New Zealand
Registered: 2011-10-14
Posts: 63
Website

Re: c call function without knowing parameters

Trilby wrote:

But back OT.  I may have misread the first time.  Are you writing just the calling function, or are you also writing the function that is being passed as a function pointer?  If you are only doing the former, you cannot do this.  You must call a function with the correct number of arguments - unless it was declared as a variadic function.  If you are writing that function to - then see va_args, and you can do this.

I was probably not clear in the first post. I'm writing an interpreted language, so in c someone can register a function. This function can take any amount of arguments. I know the values to pass for the arguments, but not how. It looks like dyncall as suggested by progandy can do this for me


HP DV6 + Arch + Openbox

Offline

#11 2013-03-22 05:33:01

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 13,436

Re: c call function without knowing parameters

Trilby,
I am not sure I agree with you.  Look at this code sample that is in the wikipedia page I linked in my last post:

void (*callback)(void *);
 
int main(void)
{
        MyMsg msg1;
        msg1.appId = 100;
        strcpy(msg1.msgbody, "This is a test\n");
 
        /*
         * Assign the address of the function 'myfunc' to the function
         * pointer 'callback'
         */
        callback = (void *)myfunc;
 
        /*
         * Call the function
         */
        callback((MyMsg*)&msg1);
 
        return 0;
}

Look how callback is defined


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
You assume people are rational and influenced by evidence.  You must not work with the public much. -- Trilby
----
How to Ask Questions the Smart Way

Offline

#12 2013-03-22 05:33:09

Trilby
Forum Moderator
From: Massachusetts, USA
Registered: 2011-11-29
Posts: 14,211
Website

Re: c call function without knowing parameters

@Benny, Cool, I had never heard of dyncall, but I just read a (very) little on it - it seems to do basically what my assembly suggestion was getting at  - but it does the dirty work for you.  I'll have to remember that one.

ewaller, was that to disagree with my claim that the number of parameters must be correct?  That callback example, and the call, both have one parameter.  It is typecast from a void * to a different pointer - but a pointer is a pointer, and types are often easily cast to other types.  It's still the same number of parameters (i.e., one).

Last edited by Trilby (2013-03-22 05:36:20)


InterrobangSlider
• How's my coding? See this page.
• How's my moderating? Feel free to email any concerns, complaints, or objections.

Offline

#13 2013-03-22 06:16:06

MagicSkyFairy
Member
Registered: 2013-03-14
Posts: 79

Re: c call function without knowing parameters

ewaller wrote:

Trilby,
I am not sure I agree with you.  Look at this code sample that is in the wikipedia page I linked in my last post:

void (*callback)(void *);
 
int main(void)
{
        MyMsg msg1;
        msg1.appId = 100;
        strcpy(msg1.msgbody, "This is a test\n");
 
        /*
         * Assign the address of the function 'myfunc' to the function
         * pointer 'callback'
         */
        callback = (void *)myfunc;
 
        /*
         * Call the function
         */
        callback((MyMsg*)&msg1);
 
        return 0;
}

Look how callback is defined

I'm confused.  Shouldn't it be callback = &myfunc?

That would give it the address location of myfinc, if I'm not mistaken...

Last edited by MagicSkyFairy (2013-03-22 06:16:38)


I have wasted atleast a second of your time by making you read my signature.

Offline

#14 2013-03-22 11:51:52

Lekensteyn
Member
From: Netherlands
Registered: 2012-06-19
Posts: 181
Website

Re: c call function without knowing parameters

The `&` is optional for functions. A "function" is already an address, there is no other meaning.

Offline

Board footer

Powered by FluxBB