declare

Programming, for all ages and all languages.
Post Reply
lama
Member
Member
Posts: 83
Joined: Thu Apr 16, 2009 8:41 am

declare

Post 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.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: declare

Post 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?
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Gigasoft
Member
Member
Posts: 855
Joined: Sat Nov 21, 2009 5:11 pm

Re: declare

Post 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
};
User avatar
qw
Member
Member
Posts: 792
Joined: Mon Jan 26, 2009 2:48 am

Re: declare

Post 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
Gigasoft
Member
Member
Posts: 855
Joined: Sat Nov 21, 2009 5:11 pm

Re: declare

Post 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.
lama
Member
Member
Posts: 83
Joined: Thu Apr 16, 2009 8:41 am

Re: declare

Post 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?
User avatar
qw
Member
Member
Posts: 792
Joined: Mon Jan 26, 2009 2:48 am

Re: declare

Post by qw »

You don't. The exact beginning of the compiled code is probably not a function.
Post Reply