array of pointers on functions

  • Thread starter Evangelista Sami
  • Start date
E

Evangelista Sami

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
 
R

Richard Heathfield

Evangelista said:
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;
}
 
P

pete

Evangelista said:
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 */
 
J

Julian V. Noble

Evangelista said:
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(.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
(e-mail address removed)
^^^^^^^^^^^^^^^^^^
http://galileo.phys.virginia.edu/~jvn/

"Science knows only one commandment: contribute to science."
-- Bertolt Brecht, "Galileo".
 
J

Julian V. Noble

Evangelista said:
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(.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
(e-mail address removed)
^^^^^^^^^^^^^^^^^^
http://galileo.phys.virginia.edu/~jvn/

"Science knows only one commandment: contribute to science."
-- Bertolt Brecht, "Galileo".
 
B

Barry Schwarz

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>>
 
M

Micah Cowan

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
 
P

Peter Nilsson

Richard Heathfield said:
Evangelista said:
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.
 
R

Richard Heathfield

Peter said:
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.
 
P

Peter Nilsson

Richard Heathfield said:
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?
 
R

Richard Heathfield

Peter said:
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.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top