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.
4 1895
A function like this: - void func01(unsigned char*, int, int);
-
would have a function pointer like this: - void (*pf)(unsigned char*, int, int);
Therefore, an array of 10 of these pointers would be: - void (*pf[10])(unsigned char*, int, int);
-
-
Then you can:
and your call would be: - unsigned char ch = 0;
-
-
pf[0](&ch,1,2);
Off you go.
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
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
Firstly, this: - void (*ptr)(BYTE*, int, int);
should not be in a header file since this creates a pointer variable.
Secondly, this: - typedef
-
void (*pf[10])(unsigned char*, int, int);
-
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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. ...
|
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...
|
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...
|
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?
...
|
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...
|
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;
|
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...
|
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.
|
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
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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...
| |