Array of function pointers - help!

M

M Turo

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.


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.


Anyone willing to help me see the light?

Regards,
Mark.
 
C

Charles Bailey

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?
 
S

santosh

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.
 
R

Richard

Charles Bailey said:
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.
 
M

Mark Bluemel

M said:
(*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.
 
C

Charles Bailey

Charles Bailey said:
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.
 
O

Old Wolf

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];
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,165
Latest member
JavierBrak
Top