Page 1 of 1

declare

Posted: Tue Mar 16, 2010 1:01 pm
by lama
hi, is it possible to do this in c? (as it can be done in nasm for ex.)
dd func
func:
thanks for the answer.

Re: declare

Posted: Tue Mar 16, 2010 1:37 pm
by Combuster
If you were a good C programmer like you should (You are asking it in the os development forum), you'd know the answer is yes.

Does the type void(*)(void) mean anything to you?

Re: declare

Posted: Tue Mar 16, 2010 1:45 pm
by Gigasoft
To declare a variable which points to a function, declare the function prototype with the asterisk and function name enclosed in parentheses, like this:
void (*FunctionPtr)();

You can also declare arrays of function pointers like this. And by using a typedef, you can avoid having to type the function declaration over and over when using it in multiple places. For example:

Code: Select all

typedef int (*RqFn)(void*,WA_REPLYDATA*,CWebApCtl*,int*);
RqFn RequestFns[]={
        (RqFn)RqAbort
};

Re: declare

Posted: Tue Mar 16, 2010 2:46 pm
by qw
Gigasoft wrote:To declare a variable which points to a function, declare the function prototype with the asterisk and function name enclosed in parentheses, like this:
void (*FunctionPtr)();

You can also declare arrays of function pointers like this. And by using a typedef, you can avoid having to type the function declaration over and over when using it in multiple places. For example:

Code: Select all

typedef int (*RqFn)(void*,WA_REPLYDATA*,CWebApCtl*,int*);
RqFn RequestFns[]={
        (RqFn)RqAbort
};
I'd recommend not to use the cast "(RqFn)RqAbort" but plain "RqAbort" so the compiler may issue an error when RqAbort happens to be of a different prototype.

Roel

Re: declare

Posted: Tue Mar 16, 2010 5:14 pm
by Gigasoft
Well, in the program which that was taken from, each function in the table has parameters 1 and 2 pointing to different structures, as each function processes a different type of request from another application, where parameter 1 points to the request parameters and parameter 2 to the buffer which will receive the reply.

Re: declare

Posted: Fri Mar 19, 2010 1:48 pm
by lama
void (*ptr)() = &func for func with no arguments and returning void i guess, thanks for the quick answer. but there is one more thing, which i want to do with that pointer. i need to have this pointer at the exact begining of compiled code. how can i do that?

Re: declare

Posted: Fri Mar 19, 2010 2:04 pm
by qw
You don't. The exact beginning of the compiled code is probably not a function.