473,471 Members | 2,533 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

array of pointers on functions

hello

i haven't touch any C code for long time and i dont remember how to
declare an array of pointers on functions. i have tried this :
-----------------------
35 void firing_1
36 (firing f,
37 int *old_m,
38 int *new_m);
39
40 const void (*firing_table[1])
41 (firing,
42 int *,
43 int *) =
44 {
45 firing_1
46 };
------------------------

but the compiler says :
__firings__.h:46: warning: initialization from incompatible pointer
type

my gcc version is 2.8.1.

any idea?

thanks for any response
Nov 13 '05 #1
10 4624
Evangelista Sami wrote:
hello

i haven't touch any C code for long time and i dont remember how to
declare an array of pointers on functions. i have tried this :
-----------------------
35 void firing_1
36 (firing f,
37 int *old_m,
38 int *new_m);
39
40 const void (*firing_table[1])
41 (firing,
42 int *,
43 int *) =
44 {
45 firing_1
46 };
------------------------

but the compiler says :
__firings__.h:46: warning: initialization from incompatible pointer
type


Typedefs usually help in this kind of situation.

typedef void FIRING_FUNCTION(firing f, int *old_m, int *new_m);

FIRING_FUNCTION firing_1;

int main(void)
{
const FIRING_FUNCTION *firing_table[1];

firing_table[0] = firing_1;

return 0;
}
--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #2
Evangelista Sami wrote:

hello

i haven't touch any C code for long time and i dont remember how to
declare an array of pointers on functions. i have tried this :
-----------------------
35 void firing_1
36 (firing f,
37 int *old_m,
38 int *new_m);
39
40 const void (*firing_table[1])
41 (firing,
42 int *,
43 int *) =
44 {
45 firing_1
46 };
------------------------

but the compiler says :
__firings__.h:46: warning: initialization from incompatible pointer
type

my gcc version is 2.8.1.

any idea?/* BEGIN new.c */


#include <stdio.h>
#include <math.h>

#define FUNCTION_LIST { sin , cos, tan }
#define STRING_LIST {"sin", "cos", "tan"}
#define PI_OVER_4 (3.14159265 / 4)
#define FUNCTIONS (sizeof function / sizeof *function)

int main(void)
{
double (*function[])(double) = FUNCTION_LIST;
char *string[] = STRING_LIST;
size_t result;

for (result = 0; result != FUNCTIONS; ++result) {
printf("%s(%f) is %f\n",
string[result], PI_OVER_4, function[result](PI_OVER_4));
}
return 0;
}

/* END new.c */

--
pete
Nov 13 '05 #3
Evangelista Sami wrote:

hello

i haven't touch any C code for long time and i dont remember how to
declare an array of pointers on functions. i have tried this :
-----------------------
35 void firing_1
36 (firing f,
37 int *old_m,
38 int *new_m);
39
40 const void (*firing_table[1])
41 (firing,
42 int *,
43 int *) =
44 {
45 firing_1
46 };
------------------------

but the compiler says :
__firings__.h:46: warning: initialization from incompatible pointer
type

my gcc version is 2.8.1.

any idea?

thanks for any response


Jirka Klaue kindly showed me this:

double (*f[])(double) = {sin, cos, log, exp}; // array of pointers
for (i=0; i<4; i++) printf("%f\n", f[i](.5)); // ex. of use

The functions can be anything, as long as they are consistent
in the type(s) of their argument(s), and in the type they return.

Probably a good idea to prototype any functions you want to point
to _before_ you point to them in the table ;-)

--
Julian V. Noble
Professor Emeritus of Physics
jv*@lessspamformother.virginia.edu
^^^^^^^^^^^^^^^^^^
http://galileo.phys.virginia.edu/~jvn/

"Science knows only one commandment: contribute to science."
-- Bertolt Brecht, "Galileo".
Nov 13 '05 #4
Evangelista Sami wrote:

hello

i haven't touch any C code for long time and i dont remember how to
declare an array of pointers on functions. i have tried this :
-----------------------
35 void firing_1
36 (firing f,
37 int *old_m,
38 int *new_m);
39
40 const void (*firing_table[1])
41 (firing,
42 int *,
43 int *) =
44 {
45 firing_1
46 };
------------------------

but the compiler says :
__firings__.h:46: warning: initialization from incompatible pointer
type

my gcc version is 2.8.1.

any idea?

thanks for any response


Jirka Klaue kindly showed me this:

double (*f[])(double) = {sin, cos, log, exp}; // array of pointers
for (i=0; i<4; i++) printf("%f\n", f[i](.5)); // ex. of use

The functions can be anything, as long as they are consistent
in the type(s) of their argument(s), and in the type they return.

Probably a good idea to prototype any functions you want to point
to _before_ you point to them in the table ;-)

--
Julian V. Noble
Professor Emeritus of Physics
jv*@lessspamformother.virginia.edu
^^^^^^^^^^^^^^^^^^
http://galileo.phys.virginia.edu/~jvn/

"Science knows only one commandment: contribute to science."
-- Bertolt Brecht, "Galileo".
Nov 13 '05 #5
On 4 Oct 2003 04:33:20 -0700, ev******@cnam.fr (Evangelista Sami)
wrote:
hello

i haven't touch any C code for long time and i dont remember how to
declare an array of pointers on functions. i have tried this :
-----------------------
35 void firing_1
36 (firing f,
37 int *old_m,
38 int *new_m);
39
40 const void (*firing_table[1])
41 (firing,
42 int *,
43 int *) =
44 {
45 firing_1
46 };
------------------------

but the compiler says :
__firings__.h:46: warning: initialization from incompatible pointer
type

my gcc version is 2.8.1.

any idea?

thanks for any response


Mine processed it without any diagnostics at all once I provided a
typedef for firing. Did you cut and paste the code or re-type it?
<<Remove the del for email>>
Nov 13 '05 #6
ev******@cnam.fr (Evangelista Sami) writes:
hello

i haven't touch any C code for long time and i dont remember how to
declare an array of pointers on functions. i have tried this :
-----------------------
35 void firing_1
36 (firing f,
37 int *old_m,
38 int *new_m);
39
40 const void (*firing_table[1])
41 (firing,
42 int *,
43 int *) =
44 {
45 firing_1
46 };
------------------------

but the compiler says :
__firings__.h:46: warning: initialization from incompatible pointer
type

my gcc version is 2.8.1.

any idea?

thanks for any response


You have an array[1] of pointers to functions... returning const
void?!?

Maybe you wanted:

void (* const firing_table[1])(firing, int *, int *) = { firing_1 };

Array[1] of const pointers to functions... returning void.

BTW, what good is a single-element array in this case?

-Micah
Nov 13 '05 #7
"Richard Heathfield" <do******@address.co.uk.invalid> wrote in message
news:bl**********@sparta.btinternet.com...
Evangelista Sami wrote:
hello

i haven't touch any C code for long time and i dont remember how to
declare an array of pointers on functions. i have tried this :
-----------------------
35 void firing_1
36 (firing f,
37 int *old_m,
38 int *new_m);
39
40 const void (*firing_table[1])
41 (firing,
42 int *,
43 int *) =
44 {
45 firing_1
46 };
------------------------

but the compiler says :
__firings__.h:46: warning: initialization from incompatible pointer
type
Typedefs usually help in this kind of situation.

typedef void FIRING_FUNCTION(firing f, int *old_m, int *new_m);

FIRING_FUNCTION firing_1;


I have an aversion to declaring function prototypes like that as they look
far too much like object declarations. YMMV
int main(void)
{
const FIRING_FUNCTION *firing_table[1];
I'm not so sure that const is a good idea.

6.7.3p8: "...If the specification of a function type includes any type
qualifiers, the behavior is undefined."
firing_table[0] = firing_1;
This assignment would presumably require a diagnostic as it discards
qualifiers.

return 0;
}


--
Peter
Nov 13 '05 #8
Peter Nilsson wrote:
"Richard Heathfield" <do******@address.co.uk.invalid> wrote in message
news:bl**********@sparta.btinternet.com...

Typedefs usually help in this kind of situation.

typedef void FIRING_FUNCTION(firing f, int *old_m, int *new_m);

FIRING_FUNCTION firing_1;
I have an aversion to declaring function prototypes like that as they look
far too much like object declarations. YMMV


MMDIV, yes. My counter-aversion is to concealing pointer types inside
typedefs.
int main(void)
{
const FIRING_FUNCTION *firing_table[1];


I'm not so sure that const is a good idea.


Neither am I. Not sure how it got in there. Sorry about that.
--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #9
"Richard Heathfield" <do******@address.co.uk.invalid> wrote in message
news:bl**********@sparta.btinternet.com...
Peter Nilsson wrote:
"Richard Heathfield" <do******@address.co.uk.invalid> wrote in message

typedef void FIRING_FUNCTION(firing f, int *old_m, int *new_m);

FIRING_FUNCTION firing_1;


I have an aversion to declaring function prototypes like that as they look far too much like object declarations. YMMV


MMDIV, yes. ...


Why not use the far less ambiguous explicit prototype...?

void firing_1(firing, int *, int *);

What do you gain from the use of the typedef in a function declaration?

--
Peter
Nov 13 '05 #10
Peter Nilsson wrote:
"Richard Heathfield" <do******@address.co.uk.invalid> wrote in message
news:bl**********@sparta.btinternet.com...
Peter Nilsson wrote:
>
> I have an aversion to declaring function prototypes
> like that as they look
> far too much like object declarations. YMMV


MMDIV, yes. ...


Why not use the far less ambiguous explicit prototype...?

void firing_1(firing, int *, int *);

What do you gain from the use of the typedef in a function declaration?


It makes defining arrays of function pointers rather simpler. In some
circumstances, it can also make the relationship between a bunch of
function prototypes more obvious. Example from some recent demo code for
Win32:

/* this is in a library header */
typedef LRESULT MW_MESSAGE_HANDLER(HWND,
UINT,
WPARAM,
LPARAM);

/* these are in an application header */
MW_MESSAGE_HANDLER VigenereCreate;
MW_MESSAGE_HANDLER VigenereResize;
MW_MESSAGE_HANDLER VigenereCommand;
MW_MESSAGE_HANDLER VigenereDestroy;

But no, I don't typedef every single function type that I use. I do it when
it seems to me to be The Right Thing.

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
Rudimentary Win32 programming tutorial:
http://www.rjgh.co.uk/prg/c/windows/index.html
Nov 13 '05 #11

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...
5
by: Steve | last post by:
Can anyone tell me if I can have an array of functions that take a variable number of parameters? If it is possible I'd like to know how to declare the array and the functions as its elements. I am...
17
by: ypjofficial | last post by:
Hello All, I have read in many c++ literature that vtable is nothing but an array of pointer to virtual functions inside a class.And the class where the virtual function/s are declared stores the...
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? ...
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;
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
26
by: aruna.mysore | last post by:
Hi all, I have a specific problem passing a function pointer array as a parameter to a function. I am trying to use a function which takes a function pointer array as an argument. I am too sure...
5
by: Immortal Nephi | last post by:
I would like to design an object using class. How can this class contain 10 member functions. Put 10 member functions into member function pointer array. One member function uses switch to call...
3
by: Ramesh | last post by:
Hi, I am trying to create an array of pointers to member functions inside my class. When I created a global array of type pfn & initialized with member functions and copy it back to the member...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.