472,808 Members | 1,936 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,808 software developers and data experts.

Function pointer tables

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
9 8903
p=table[0];

should be

p = table;

or

p = &table[0];
Mar 24 '08 #2
/* 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
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
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

<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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: John Collyer | last post by:
Hi, In assembly language you can use a lookup table to call functions. 1. Lookup function address in table 2. Call the function Like: CALL FUNCTION
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...
7
by: Frank-René Schäfer | last post by:
Case: -- class X has occupies tiny amount of memory: sizeof(X) is only a little greater than sizeof(void*). -- X instantiates thousands of objects and memory does matter. -- The class has...
11
by: Rajesh | last post by:
Dear All, Please let me know the advantage of function pointer? Is it fast calling function using function pointer? Is it possible to use function pointer to optimise code? Thanks and regards...
1
by: Mike | last post by:
In C, we can typedef pointer to functions, and therefore use function tables. But what's the advantage of using function table? Thanks, Mike
2
by: drdave | last post by:
Hi I have a situation where I call a function and return a dataset, this works fine but I am trying to figure out the best way to send back to the call page whether or not there are records...
6
by: Todd A. Anderson | last post by:
I have a function foo of which I need to get the address. The problem is that when you say "&foo" (or just foo for that matter), you get the address of this function's entry in a jump table and...
45
by: noridotjabi | last post by:
What is the purpose of the function pointer? Why do you need a pointer to a function. I cannot really think of any application where this is the only or even easiest solution to a problem. I'm...
7
by: ghulands | last post by:
I am having trouble implementing some function pointer stuff in c++ An object can register itself for many events void addEventListener(CFObject *target, CFEventHandler callback, uint8_t...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.