473,249 Members | 1,448 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,249 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 3477
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
1
by: jon wayne | last post by:
Hi Am trying to replace the classic switch case construct (am workng on a telecom stack devlpmnt)with an array of function ptrs. The problm am facing is that of indexing. - the case vals can be...
3
by: kohvirus | last post by:
Well after embaressing myself and posting in the wrong fourm, I found my way to the right one and I'm hoping to seek some help. This is the program without any modifications that I am making...
4
by: Simon Schaap | last post by:
Hello, I have encountered a strange problem and I hope you can help me to understand it. What I want to do is to pass an array of chars to a function that will split it up (on every location where...
7
by: Kathy Tran | last post by:
Hi, Could you please help me how to declare an araay of pointer in C#. In my program I declared an structure public struct SEventQ { public uint uiUserData; public uint uiEvent; public uint...
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
1
by: Puddle | last post by:
Hi everyone, I'd just like to preface by saying any input or direction in this would be very much appreciated; i've been working on this endlessly and can't figure out what's going wrong. I'm...
2
by: StevenChiasson | last post by:
For the record, not a student, just someone attempting to learn C++. Anyway, the problem I'm having right now is the member function detAddress, of object controller. This is more or less, your...
33
by: Adam Chapman | last post by:
Hi, Im trying to migrate from programming in Matlab over to C. Im trying to make a simple function to multiply one matrix by the other. I've realised that C can't determine the size of a 2d...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.