471,305 Members | 1,105 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,305 software developers and data experts.

Array of function pointers - help!

Hi,

I was wondering if anyone can help. I'm want to pre-load a 'table' of
function pointers that I can call using a its arrayed index, eg (non c
code example)
pFunc[0] = func_A;
pFunc[1] = func_B;
Jul 19 '07 #1
6 3325
On 2007-07-19, M Turo <no@email.addresswrote:
Hi,

I was wondering if anyone can help. I'm want to pre-load a 'table' of
function pointers that I can call using a its arrayed index, eg (non c
code example)
pFunc[0] = func_A;
pFunc[1] = func_B;
.
.
pFunc[n] = func_x

So I tried to create the function pointer array (after reading about
function pointers in a tutorial) with:

(*ptr_func)(unsigned char)[10];

And

(*ptr_func)[10](unsigned char);

But just get compiler errors.
Array of ten pointers to functions taking and unsigned char and
returning int:
int (*ptr_func_array[10])(unsigned char);

or you could do:
int (*ptr_func_array[])(unsigned char) = { func_A, func_B, func_x };
>
I thought pointers were typically the same size as an int so that I
could assign my func pointers to an int array, but then, I wondered,
what about the parameter list.
Why? Function pointers are not ints, why not use the correct type?

Jul 19 '07 #2
On Thursday 19 Jul 2007 4:12 pm, M Turo <no@email.addresswrote in
message <qm********************************@4ax.com>:
Hi,

I was wondering if anyone can help. I'm want to pre-load a 'table'
of function pointers that I can call using a its arrayed index, eg
(non c code example)
pFunc[0] = func_A;
pFunc[1] = func_B;
.
.
pFunc[n] = func_x

So I tried to create the function pointer array (after reading
about function pointers in a tutorial) with:

(*ptr_func)(unsigned char)[10];

And

(*ptr_func)[10](unsigned char);

But just get compiler errors.
Do:

int (*fparray[SIZE])(unsigned char);
I thought pointers were typically the same size as an int [ ... ]
No. The relative sizes of pointers and integers are implementation
dependant. On many machines pointers are bigger than integers, on
others smaller than integers. Also some integer types could be
smaller than pointers while others could be larger. Pointers of
different types could also be of different sizes.

On a machine here, shorts are two bytes while ints, longs and
pointers are four bytes while long long is eight bytes.

In general you should store a value of type T in an object of type T.
Jul 19 '07 #3
Charles Bailey <us**********@hashpling.orgwrites:
On 2007-07-19, M Turo <no@email.addresswrote:
>Hi,

I was wondering if anyone can help. I'm want to pre-load a 'table' of
function pointers that I can call using a its arrayed index, eg (non c
code example)
pFunc[0] = func_A;
pFunc[1] = func_B;
.
.
pFunc[n] = func_x

So I tried to create the function pointer array (after reading about
function pointers in a tutorial) with:

(*ptr_func)(unsigned char)[10];

And

(*ptr_func)[10](unsigned char);

But just get compiler errors.

Array of ten pointers to functions taking and unsigned char and
returning int:
int (*ptr_func_array[10])(unsigned char);

or you could do:
int (*ptr_func_array[])(unsigned char) = { func_A, func_B, func_x };
>>
I thought pointers were typically the same size as an int so that I
could assign my func pointers to an int array, but then, I wondered,
what about the parameter list.

Why? Function pointers are not ints, why not use the correct type?
What is the correct type? That is not clear from your example. There is
no defined "type" and seems to be a special "type" from the syntax.
Jul 19 '07 #4
M Turo wrote:
(*ptr_func)(unsigned char)[10];

And

(*ptr_func)[10](unsigned char);
As others have indicated,
(*ptr_func[10])(unsigned char);

is the correct declaration - the [10] must go with the name of the array...

However, I'd be inclined to use a typedef, as it's less ugly.

typedef void *(*PTR_FUNC)(unsigned char);

says that PTR_FUNC is a pointer to a function taking unsigned char and
returning pointer to void, for example.

PTR_FUNC my_pointers[10];

says that "my_pointers" is an array of 10 such pointers.
Jul 19 '07 #5
On 2007-07-19, Richard <rg****@gmail.comwrote:
Charles Bailey <us**********@hashpling.orgwrites:
>On 2007-07-19, M Turo <no@email.addresswrote:
>>Hi,

I was wondering if anyone can help. I'm want to pre-load a 'table' of
function pointers that I can call using a its arrayed index, eg (non c
code example)
pFunc[0] = func_A;
pFunc[1] = func_B;
.
.
pFunc[n] = func_x

So I tried to create the function pointer array (after reading about
function pointers in a tutorial) with:

(*ptr_func)(unsigned char)[10];

And

(*ptr_func)[10](unsigned char);

But just get compiler errors.

Array of ten pointers to functions taking and unsigned char and
returning int:
int (*ptr_func_array[10])(unsigned char);

or you could do:
int (*ptr_func_array[])(unsigned char) = { func_A, func_B, func_x };
>>>
I thought pointers were typically the same size as an int so that I
could assign my func pointers to an int array, but then, I wondered,
what about the parameter list.

Why? Function pointers are not ints, why not use the correct type?

What is the correct type? That is not clear from your example. There is
no defined "type" and seems to be a special "type" from the syntax.
I'm not sure I follow you're driving at.

The OP didn't define what type his functions were, I inferred from
his syntax guesses that the most likely type was function taking
unsigned char and returning int (just because the return type was not
mentioned), but there was some question as to whether his functions
were of a single type so I asked the question to see if there was a
reason that he couldn't use an array of function pointers. Perhaps I
didn't make myself clear or perhaps I've misunderstood your
question/objection.

If my guess was correct, then the correct type for the function
pointer array would be int(*[10])(unsigned char), I think (!), I
originally phrased my reply as an example variable declaration as I
thought that this would be more helpful, matching what OP was
attempting to write. I should have made this clearer.
Jul 19 '07 #6
On Jul 20, 12:15 am, Mark Bluemel <mark_blue...@pobox.comwrote:
However, I'd be inclined to use a typedef, as it's less ugly.

typedef void *(*PTR_FUNC)(unsigned char);

says that PTR_FUNC is a pointer to a function taking unsigned char
and returning pointer to void, for example.

PTR_FUNC my_pointers[10];

says that "my_pointers" is an array of 10 such pointers.
I would prefer to avoid pointer typedefs:
typedef void *FUNC(unsigned char);
FUNC *my_pointers[10];

Jul 19 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by kohvirus | last post: by
4 posts views Thread by Simon Schaap | last post: by
7 posts views Thread by Kathy Tran | last post: by
1 post views Thread by Puddle | last post: by
33 posts views Thread by Adam Chapman | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.