By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,847 Members | 2,392 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,847 IT Pros & Developers. It's quick & easy.

Function pointer tables

P: n/a
I'm having problems with the following code.

I have a table of function pointers, and a function pointer variable p which
steps through the functions.

But I'm not allowed to increment the variable using ++p.

What's the problem here?

Also, I may not be interested in returning from any of these functions (each
will call the next according to some global variable). Any recommended way
of doing this (throwing away return address) other than a crude asm("pop
R")?

Thanks,

Bart.
#include <stdio.h>
#include <stdlib.h>

void f1(void);
void f2(void);
void f3(void);
void f4(void);

int main(void)
{
void (*table[])(void)={&f1,&f2,&f3,&f4}; /* table of function pointers */
void (*p)(void); /* pointer to one of the functions (I hope) */

p=table[0];

while(1)
{ (*p)(); /* Call function @p */
++p; /* COMPILE ERROR HERE */
};

}

void f1(void){puts("F1 CALLED");return;};
void f2(void){puts("F2 CALLED");return;};
void f3(void){puts("F3 CALLED");return;};
void f4(void){puts("F4 CALLED"); exit(0);};
Mar 24 '08 #1
Share this Question
Share on Google+
9 Replies


P: n/a
p=table[0];

should be

p = table;

or

p = &table[0];
Mar 24 '08 #2

P: n/a
/* Also, p needs to be a pointer to a function pointer. */

#include <stdio.h>
#include <stdlib.h>

void f1(void);
void f2(void);
void f3(void);
void f4(void);

int main(void)
{
void (*table[])(void)={&f1,&f2,&f3,&f4}; /* table of function
pointers */
void (**p)(void); /* pointer to a function pointer */

p=table;

while(1)
{
(*(*p))(); /* Call function @p */
++p;

};
}

void f1(void){puts("F1 CALLED");return;};
void f2(void){puts("F2 CALLED");return;};
void f3(void){puts("F3 CALLED");return;};
void f4(void){puts("F4 CALLED"); exit(0);};
Mar 24 '08 #3

P: n/a
On Mar 25, 9:44*am, "Bartc" <b...@freeuk.comwrote:
I'm having problems with the following code.

I have a table of function pointers, and a function
pointer variable p which steps through the functions.
Think more basically of p as a pointer to an array
element.
But I'm not allowed to increment the variable using
++p.

What's the problem here?

Also, I may not be interested in returning from any of
these functions (each will call the next according to
some global variable). Any recommended way of doing
this (throwing away return address) other than a crude
asm("pop R")?

Thanks,

Bart.

#include <stdio.h>
#include <stdlib.h>

void f1(void);
void f2(void);
void f3(void);
void f4(void);

int main(void)
{
void (*table[])(void)={&f1,&f2,&f3,&f4};
* */* table of function pointers */
This is an array of function pointers.
void (*p)(void);
* */* pointer to one of the functions (I hope) */
This is a function pointer, not a pointer to a
function pointer.
p=table[0];

while(1)
{ (*p)(); * */* Call function @p */
* ++p; * */* COMPILE ERROR HERE */

};
}

void f1(void){puts("F1 CALLED");return;};
void f2(void){puts("F2 CALLED");return;};
void f3(void){puts("F3 CALLED");return;};
void f4(void){puts("F4 CALLED"); exit(0);};
The extra semi-colons are a syntax error too.
Simple typedefs will make this simpler.

#include <stdio.h>
#include <stdlib.h>

void f1(void) { puts("F1 CALLED"); return; }
void f2(void) { puts("F2 CALLED"); return; }
void f3(void) { puts("F3 CALLED"); return; }
void f4(void) { puts("F4 CALLED"); exit(0); }

typedef void (*func_ptr)(void);

int main(void)
{
func_ptr table[] = { f1, f2, f3, f4 };
func_ptr *p;

for (p = table; p < table + 4; p++)
(*p)();

return 0;
}

--
Peter
Mar 24 '08 #4

P: n/a
On Mar 25, 12:44 am, "Bartc" <b...@freeuk.comwrote:
I'm having problems with the following code.

I have a table of function pointers, and a function pointer variable p which
steps through the functions.

But I'm not allowed to increment the variable using ++p.

What's the problem here?

Also, I may not be interested in returning from any of these functions (each
will call the next according to some global variable). Any recommended way
of doing this (throwing away return address) other than a crude asm("pop
R")?

Thanks,

Bart.

#include <stdio.h>
#include <stdlib.h>

void f1(void);
void f2(void);
void f3(void);
void f4(void);

int main(void)
{
void (*table[])(void)={&f1,&f2,&f3,&f4}; /* table of function pointers */
void (*p)(void); /* pointer to one of the functions (I hope) */
You need void (**p)(void)
p=table[0];
and here, p = table;

The reason for this is simple:

table[0] is a void (*)(void), so p must be void (**)(void)
(like string[0] is char and p must be char *)

<snip>
Mar 24 '08 #5

P: n/a

<vi******@gmail.comwrote in message
news:80**********************************@e6g2000p rf.googlegroups.com...
On Mar 25, 12:44 am, "Bartc" <b...@freeuk.comwrote:
>I'm having problems with the following code.

I have a table of function pointers, and a function pointer variable p
which
steps through the functions.
>void (*table[])(void)={&f1,&f2,&f3,&f4}; /* table of function pointers
*/
void (*p)(void); /* pointer to one of the functions (I hope) */
You need void (**p)(void)
>p=table[0];
and here, p = table;

The reason for this is simple:

table[0] is a void (*)(void), so p must be void (**)(void)
(like string[0] is char and p must be char *)
Ok, thanks for the replies.

So with ++p I was attempting to step to the next (actual) function, not the
next table entry. I understand now. C's declaration syntax isn't any clearer
though!

--
Bart
Mar 25 '08 #6

P: n/a
Bartc wrote:
Ok, thanks for the replies.

So with ++p I was attempting to step to the next (actual) function, not the
next table entry. I understand now.
Incrementing a pointer is syntactically correct. However, incrementing a
function pointer is not semantically possible.

That's more or less the case of arrays of functions. Theoretically, you
could do:
int func_array[6](int argument);
Meaning "func_array is an array of 6 functions, each taking one int and
returning int". This has no meaning in C semantics, however, because there
aren't objects of the type "array of functions".
C's declaration syntax isn't any clearer though!
C syntax is clear. It's not, however, the most intuitive!

The ideia is simple. You have an "array of foo" (where foo is a type). It's
(array's) symbol behaves arithmetically as a "pointer to foo". This means that:
- The operator [], that apparently is an indexing operator, actually acts
on pointers (and objects that arithmetically behave like pointers), by
summing it with the "index" and derreferencing it (that said, thing[i] is
synctatic sugar for *(thing+i) )
- If you need to point to an array, you really want a pointer to the first
element of that array. For example, if you have an object of type "array of
chars" and you need to point to it, you'll need a "pointer to char".
Similary, if you have an object of type "array of pointers to functions",
and you need to point to it, you will need a "pointer to pointer to function".

Rather simple, but still has to be explained!

JJ
Mar 27 '08 #7

P: n/a
Bartc wrote:
>
I'm having problems with the following code.

I have a table of function pointers, and a function pointer variable p which
steps through the functions.

But I'm not allowed to increment the variable using ++p.

What's the problem here?

Also, I may not be interested in returning from any of these functions (each
will call the next according to some global variable). Any recommended way
of doing this (throwing away return address) other than a crude asm("pop
R")?

Thanks,

Bart.

#include <stdio.h>
#include <stdlib.h>

void f1(void);
void f2(void);
void f3(void);
void f4(void);

int main(void)
{
void (*table[])(void)={&f1,&f2,&f3,&f4}; /* table of function pointers */
void (*p)(void); /* pointer to one of the functions (I hope) */

p=table[0];

while(1)
{ (*p)(); /* Call function @p */
++p; /* COMPILE ERROR HERE */
};

}

void f1(void){puts("F1 CALLED");return;};
void f2(void){puts("F2 CALLED");return;};
void f3(void){puts("F3 CALLED");return;};
void f4(void){puts("F4 CALLED"); exit(0);};
/* BEGIN new.c */

#include <stdio.h>
#include <stdlib.h>

void f1(void);
void f2(void);
void f3(void);
void f4(void);

int main(void)
{
void (*table[])(void) = {
f1, f2, f3, f4
};
void (**p)(void);

p = table;
while (p != table + sizeof table / sizeof *table) {
(*p++)();
}
return 0;
}

void f1(void)
{
puts("F1 CALLED");
}

void f2(void)
{
puts("F2 CALLED");
}

void f3(void)
{
puts("F3 CALLED");
}

void f4(void)
{
puts("F4 CALLED");
}

/* END new.c */

--
pete
Mar 28 '08 #8

P: n/a
On Mar 28, 1:00*pm, pete <pfil...@mindspring.comwrote:
Bartc wrote:
I'm having problems with the following code.
I have a table of function pointers, and a function pointer variable p which
steps through the functions.
But I'm not allowed to increment the variable using ++p.
What's the problem here?
Also, I may not be interested in returning from any of these functions (each
will call the next according to some global variable). Any recommended way
of doing this (throwing away return address) other than a crude asm("pop
R")?
Thanks,
Bart.
#include <stdio.h>
#include <stdlib.h>
void f1(void);
void f2(void);
void f3(void);
void f4(void);
int main(void)
{
void (*table[])(void)={&f1,&f2,&f3,&f4}; * */* table of function pointers */
void (*p)(void); * */* pointer to one of the functions (I hope) */
p=table[0];
while(1)
{ (*p)(); * */* Call function @p */
* ++p; * */* COMPILE ERROR HERE */
};
}
void f1(void){puts("F1 CALLED");return;};
void f2(void){puts("F2 CALLED");return;};
void f3(void){puts("F3 CALLED");return;};
void f4(void){puts("F4 CALLED"); exit(0);};

/* BEGIN new.c */

#include <stdio.h>
#include <stdlib.h>

void f1(void);
void f2(void);
void f3(void);
void f4(void);

int main(void)
{
* * void (*table[])(void) = {
* * * * f1, f2, f3, f4
* * }; *
* * void (**p)(void); *

* * p = table;
* * while (p != table + sizeof table / sizeof *table) {
* * * * (*p++)();
* * }
* * return 0;

}

void f1(void)
{
* * puts("F1 CALLED");

}

void f2(void)
{
* * puts("F2 CALLED");

}

void f3(void)
{
* * puts("F3 CALLED");

}

void f4(void)
{
* * puts("F4 CALLED");

}

/* END new.c */
I like to use typedefs for arrays of function pointers.

#include<math.h>
#include<stdio.h>
typedef double (*f_t) (double);
static f_t f[] = {log, log10, sqrt, cos, cosh, exp, sin, sinh, tan,
tanh, 0};
int main(void)
{
int i;
f_t *flist = f;
for (i = 0; f[i]; i++) {
printf("function %d of 0.5 = %g\n", i, f[i] (0.5));
printf("function %d of 0.5 = %g\n", i, (*f[i]) (0.5));
}
while (*flist) {
f_t ff = *flist;
printf("current function of 0.5 = %g\n", ff(0.5));
flist++;
} return 0;
}

Mar 28 '08 #9

P: n/a
pete <pf*****@mindspring.comwrites:
I've been softening my stance on when to use typedefs lately.
[...]
I've recently considered that simplefying declarations,
is probably sufficient reason to use a typedef.
I agree.

One special case: I usually use a typedef to declare a function
type that I am going to use for declaring a pointer-to-function.
An extreme example is that I find
typedef void callback_func(int event);
callback_func *set_callback(callback_func *);
much easier to read than the equivalent that does not use a
typedef.
--
"This is a wonderful answer.
It's off-topic, it's incorrect, and it doesn't answer the question."
--Richard Heathfield
Mar 29 '08 #10

This discussion thread is closed

Replies have been disabled for this discussion.