Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.os.msdos.programmer > #549 > unrolled thread
| Started by | Harry Potter <rose.joseph12@yahoo.com> |
|---|---|
| First post | 2012-05-03 07:12 -0700 |
| Last post | 2012-05-04 06:13 -0700 |
| Articles | 4 — 3 participants |
Back to article view | Back to comp.os.msdos.programmer
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
| From | Harry Potter <rose.joseph12@yahoo.com> |
|---|---|
| Date | 2012-05-03 07:12 -0700 |
| Subject | Array of Functions in C? |
| Message-ID | <22294500.461.1336054343786.JavaMail.geo-discussion-forums@vbep19> |
How do I create an array of pointers to functions in ANSI/ISO C?
[toc] | [next] | [standalone]
| From | pete@nospam.demon.co.uk |
|---|---|
| Date | 2012-05-03 18:26 +0000 |
| Message-ID | <1336069582snz@nospam.demon.co.uk> |
| In reply to | #549 |
In article <22294500.461.1336054343786.JavaMail.geo-discussion-forums@vbep19>
rose.joseph12@yahoo.com "Harry Potter" writes:
> How do I create an array of pointers to functions in ANSI/ISO C?
Define (typedef) a function pointer, and create an array of these.
For example (K&R) and assuming all functions return 'int':
typedef int (*PFI)(); /* pointer to function returning int */
PFI ftable[] = { foo, bar, ... };
and call with
int result = (*ftable[i])();
For ANSI/ISO you'll need to prototype foo(), bar() etc. and adjust the
typedef accordingly.
Pete
--
Believe those who are seeking the truth.
Doubt those who find it. - André Gide
[toc] | [prev] | [next] | [standalone]
| From | "Rod Pemberton" <do_not_have@notemailnot.cmm> |
|---|---|
| Date | 2012-05-04 02:37 -0400 |
| Message-ID | <jnvte1$k46$1@speranza.aioe.org> |
| In reply to | #549 |
"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
[toc] | [prev] | [next] | [standalone]
| From | Harry Potter <rose.joseph12@yahoo.com> |
|---|---|
| Date | 2012-05-04 06:13 -0700 |
| Message-ID | <14401593.1114.1336137198590.JavaMail.geo-discussion-forums@vbki8> |
| In reply to | #552 |
I want to define the array as static (not the keyword but the scope, even though the keyword is used) at compile time rather than using mallc() to allocate them. The individual functions are defined elsewhere and declared in a peviously-#included header. I want to call the function via subscript, i.e.:
c=(*foo) [2] (var);
I figure it would be faster and more efficient to do this instead of using switch() to delegate a call to a specific function based on a zero-indexed value. If you need more clarifiication, I can reveal the bugged code either later today or starting on Monday. Thank you for listening. :)
--
Joseph Rose, a.k.a. Harry Potter
Working magic in the computer community
[toc] | [prev] | [standalone]
Back to top | Article view | comp.os.msdos.programmer
csiph-web