473,399 Members | 3,656 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,399 software developers and data experts.

Assigning functions to an array of function pointers

I'm trying to create an array of function pointers and then assign compartilbe functions to them, so I can just call *pf[0](xxx);

The functions are all of the type

void func01(unsigned char*, int, int)

how would I create an array of function pointers and assign the address of the functions to them? So I could call them like

ptrToFunction[i](charBuffer, 10, 20);

I've read a bit on line and I thought I could do it but so far I've failed.

It seems trivial and I feel I'm close but close isn't good enough.


I'd like to assign the fuction addresses like this:
for (int i=0; i<10; i++)
if (i==1)
ptrToFunction[i]=func01;
if (i==2)
ptrToFunction[i]=func02;
etc. The actual logic is somewhat different than this but this close.
May 13 '13 #1
4 1895
weaknessforcats
9,208 Expert Mod 8TB
A function like this:

Expand|Select|Wrap|Line Numbers
  1. void func01(unsigned char*, int, int);
  2.  
would have a function pointer like this:

Expand|Select|Wrap|Line Numbers
  1. void (*pf)(unsigned char*, int, int);
Therefore, an array of 10 of these pointers would be:

Expand|Select|Wrap|Line Numbers
  1. void (*pf[10])(unsigned char*, int, int);
  2.  
  3.  
Then you can:

Expand|Select|Wrap|Line Numbers
  1. pf[0] = func01;
  2.  
and your call would be:


Expand|Select|Wrap|Line Numbers
  1. unsigned char ch = 0;
  2.  
  3.     pf[0](&ch,1,2);
Off you go.
May 14 '13 #2
I put this in the header file:
typedef
void (*pf[10])(unsigned char*, int, int);

and this in the cpp: (they are both class members)

for (int i=0; i<10; i++)
pf[i] = func01;

(ignoring that pf[i] will be assigned the same function address for all values of 'i'. However this caused errors:

error C3867: 'C::func01': function call missing argument list; use '&C::func01' to create a pointer to member

Removing the typedef, I still get:

error C2440: '=' : cannot convert from 'void (__thiscall C::* )(unsigned char *,int,int)' to 'void (__cdecl *)(unsigned char *,int,int)'
1> There is no context in which this conversion is possible
May 14 '13 #3
Actually, forget the array, as I'm making some more basic type of error. In the header file I have this:
void (*ptr)(BYTE*, int, int);

And in the implementation file I have this:

ptr=&func01;

OR
C::ptr=&C::func01;

etc. Still get errors

And this is the error I get:
error C2440: '=' : cannot convert from 'void (__thiscall C::* )(unsigned char *,int,int)' to 'void (__cdecl *)(unsigned char *,int,int)'
1> There is no context in which this conversion is possible
May 14 '13 #4
weaknessforcats
9,208 Expert Mod 8TB
Firstly, this:
Expand|Select|Wrap|Line Numbers
  1. void (*ptr)(BYTE*, int, int);
should not be in a header file since this creates a pointer variable.
Secondly, this:
Expand|Select|Wrap|Line Numbers
  1. typedef
  2.  void (*pf[10])(unsigned char*, int, int);
  3.  
says that pf is a type. pf[10] cannot be a type since an array is not a typs is a collection f a type. Also, s typedef does not create a variable. You still need to create the array.

You say you are using a class now so you had better post your class declaration and the member function that getting the errors.
May 14 '13 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Balbir Singh | last post by:
I was wondering if a function pointer pointing to an inline function, will actually expand "inline" when the function pointer is invoked. #include <iostream> #include <vector> using...
4
by: Peter Salzman | last post by:
Hi all, Since PHP is a (weakly) typed language, is it possible to overload functions based on the type of their argument? Also, are there function pointers in PHP? Here's why I'm asking. ...
7
by: Bryan Parkoff | last post by:
C/C++ Compiler encourages to limit 1,000 functions under function pointer, but I am allowed to overlimit 4,096 functions. I am just for fun to test how it works. The problem is that source code...
13
by: munni | last post by:
hi i want to write a program on function pointers where i have not written any programs using function pointers till now. i want to take an array of 26 functions which r to be pointed by this...
3
by: Bilgehan.Balban | last post by:
Hi, How do I declare an array of function pointers and assign each element of the array with public member functions of a class? Is it possible that the array is not a member of the class? ...
11
by: cps | last post by:
Hi, I'm a C programmer taking my first steps into the world of C++. I'm currently developing a C++ 3D graphics application using GLUT (OpenGL Utility Toolkit written in C) for the GUI...
6
by: M Turo | last post by:
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 = func_A; pFunc = func_B;
3
by: googlinggoogler | last post by:
Hi This should all be pretty standard C stuff, but I'm going to use terms like mouse callback to communicate what Im tyring to do. Basically I have my program whirling around in an infinite...
2
by: riddhi.mittal | last post by:
what is the difference in : typedef void (fn) (); and typedef void (*fn) (); the first one is used by my course reader for passing callback functions as arguments to other functions.
8
by: a | last post by:
Hello. Suppose I have a family of functions f_0(x) = 0*x f_1(x) = 1*x .... f_n(x) = n*x taking float and returning float (naturally, the actual functions would
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.