473,771 Members | 2,297 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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,&f 2,&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 9006
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,&f 2,&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.co mwrote:
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,&f 2,&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)(voi d);

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.co mwrote:
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,&f 2,&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******** *************** ***********@e6g 2000prf.googleg roups.com...
On Mar 25, 12:44 am, "Bartc" <b...@freeuk.co mwrote:
>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,&f 2,&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,&f 2,&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...@mindsp ring.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,&f 2,&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("functio n %d of 0.5 = %g\n", i, f[i] (0.5));
printf("functio n %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*****@mindsp ring.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(i nt event);
callback_func *set_callback(c allback_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
19033
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
10181
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 code... TCHAR myArray; DoStuff(myArray);
7
1779
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 a virtual destructor, and therefore, a pointer to a virtual function table.
11
15789
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 Rajesh
1
6250
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
1410
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 returned. What I'm tring to accomplish.. dgSearchResults.DataSource = ReportFour.GetReport4 (ddlprovlist.SelectedItem.Value,stryearval)
6
5045
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 not the address of the function itself. Are there any BKMs for getting the real address or am I going to have to write a function that looks to see whether the address is a jump instruction and if so compute the real address from the jump target?...
45
899
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 sure there are really good aplications for it I just cannot think of any, so if anyone can tell me why a pointer to a function is nessisary and when it can/should be used I would apreciate that. Thanks. Nori
7
3829
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 event); so I declared a function pointer like typedef void (CFObject::*CFEventHandler)(CFEvent *theEvent);
0
10260
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10102
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9910
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7460
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6712
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
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 we have to send another system
2
3609
muto222
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.