Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.os.msdos.programmer > #552
| From | "Rod Pemberton" <do_not_have@notemailnot.cmm> |
|---|---|
| Newsgroups | comp.os.msdos.programmer |
| Subject | Re: Array of Functions in C? |
| Date | 2012-05-04 02:37 -0400 |
| Organization | Aioe.org NNTP Server |
| Message-ID | <jnvte1$k46$1@speranza.aioe.org> (permalink) |
| References | <22294500.461.1336054343786.JavaMail.geo-discussion-forums@vbep19> |
"Harry Potter" <rose.joseph12@yahoo.com> wrote in message
news:22294500.461.1336054343786.JavaMail.geo-discussion-forums@vbep19...
> How do I create an array of pointers to functions in ANSI/ISO C?
>
Did you intend to post to comp.lang.c?
With or without using malloc() to allocate the array?
Without malloc(), you declare an array and optionally initialize it. The
only difference from a regular array is that the array type must be a
function pointer.
(FYI, my terminology is probably not as precise as those on c.l.c. would
like.)
However, that means all functions in the array must be declared to be the
same pointer type. So, all functions are declared the same way: same return
type and same parameters. I.e., all void func(void) or all int func(long,
short, char) or all whatever ... etc. The easiest way to setup the array is
to declare a typedef for a pointer to the function type. A typedef creates
a template of sorts which doesn't allocate space. When you declare the
array (or use malloc() to do so), then space is allocated for the array.
It might be possible to do different types, I'm not sure. I've never needed
that, so never learned that.
A function pointer has the following form:
'return type' (* 'name of function pointer') ('function parameters')
The difference from a normal procedure declaration is the '*' for a pointer
and the () around the function pointer.
Or, reduced, you'll notice two sets of () parens and pointer '*':
(*)()
So, a function pointer, i.e., without a procedure, to a function named NAMD
and declared as 'void NAME (void)' is:
void (* NAME)(void);
I.e., you add a '*' for a pointer ...
So, the typedef for a function pointer to a function declared as 'void NAME
(void)' is:
typedef void (* func)(void);
That declares 'func' to be a typedef for a function pointer, or a typedef of
a pointer to a function this declared as 'void NAME(void)'.
So, your array is declared just like an array and used like so:
typedef void (* func)(void);
func MYARRAY[10];
void FUNCTION1 (void)
{
/* procedure */
}
MYARRAY[1]=FUNCTION1;
Or, it's declared and initialized, again using a function declared as 'void
NAME (void)' :
typedef void (* func)(void);
void FUNCTION1 (void)
{
/* procedure */
}
void FUNCTION2 (void)
{
/* procedure */
}
func MYARRAY[]=
{
FUNCTION1,
FUNCTION2
}
In C, the syntax for declarations mimick usage. So to call a function, you
indirect the function pointer. Note that this '*' is indirection on the
function pointer stored in the 1th element of MYARRAY, i.e., MYARRAY[1].
When a function pointer is indirected, the function gets called. The '*' is
not indicating a function pointer, but the syntax is the same:
(*MYARRAY[1])(); /* calls function */
Reduced:
(*)();
FYI, sometimes, casts are needed when dealing with function pointers.
You'll can easily get lost when the casts are nested inside '(*)();' as to
which parens are for the casts and which are for the function call.
HTH,
Rod Pemberton
Back to comp.os.msdos.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Array of Functions in C? Harry Potter <rose.joseph12@yahoo.com> - 2012-05-03 07:12 -0700
Re: Array of Functions in C? pete@nospam.demon.co.uk - 2012-05-03 18:26 +0000
Re: Array of Functions in C? "Rod Pemberton" <do_not_have@notemailnot.cmm> - 2012-05-04 02:37 -0400
Re: Array of Functions in C? Harry Potter <rose.joseph12@yahoo.com> - 2012-05-04 06:13 -0700
csiph-web