473,396 Members | 1,924 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 8964
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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,...

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.