473,395 Members | 1,870 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,395 software developers and data experts.

Array of function pointers

+1 #include<stdio.h>
+2 #include <stdlib.h>
+3 typedef struct
+4 {
+5 unsigned int PID;
+6 unsigned int CID;
+7 } T_ID;
+8
+9 typedef unsigned int (*T_HANDLER)(void);
+10
+11 void Display1(void);
+12 void Display2(void);
+13
+14 int main()
+15 {
+16 T_HANDLER A_HANDLER[3] = { (T_HANDLER)Display1,
+17 (T_HANDLER)Display2};
+18
+19 (A_HANDLER[0])();
+20 (*A_HANDLER[1])();
^ Is this '*' required ?
+21 return 1;
+22 }
+23
+24 void Display1()
+25 {
+26 printf("\nDisplay1 called\n");
+27 }
+28
+29 void Display2()
+30 {
+31 printf("\nDisplay2 called\n");
+32 }
Hi,
I am new to group. I just tried to run a code with array of function pointer.
I could not understand why there is no error due to '*' at line no. 20.
Could anybody help me ?

Regards,
Sushil.
Nov 13 '05 #1
2 12656
On 4 Dec 2003, sushil wrote:
+19 (A_HANDLER[0])();
+20 (*A_HANDLER[1])();
^ Is this '*' required ?
Not really, but it doesn't harm either.

I could not understand why there is no error due to '*' at line no. 20.
Could anybody help me ?


Let's have

void f(void) {}
void (*pf)(void) = &f; /* ...= f goes as well. */

We get:

&f -> pointer to function
f -> function
pf -> pointer to function
*pf -> function

Now, like arrays, functions usually decay to pointers to them:

(&f)() -> (pointer to function)()
(f)() -> (function)() -> (pointer to function)()
(pf)() -> (pointer to function)()
(*pf)() -> (function)() -> (pointer to function)()

As well we get:

*(&f) -> *(pointer to function) -> function
*(f) -> *(function) -> *(pointer to function) -> function
*(pf) -> *(pointer to function) -> function
*(*pf) -> *(function) -> *(pointer to function) -> function

And further:

*(*(&f)) -> *(function) -> *(pointer to function) -> function
*(*(f)) -> *(function) -> *(pointer to function) -> function
*(*(pf)) -> *(function) -> *(pointer to function) -> function
*(*(*pf)) -> *(function) -> *(pointer to function) -> function

etc.

So you can pretty much replace any function() with a (******function)()
and it's the same thing.

The array you have is just an extra layer of complexity.

Nov 13 '05 #2
On Thu, 4 Dec 2003, Jarno A Wuolijoki wrote:
Now, like arrays, functions usually decay to pointers to them:


Oh, let's fix an "implied" thinko before anyone else catches:

Arrays will of course decay to pointers to their elements, not
to pointers to itself.

Nov 13 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

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...
4
by: Isaac | last post by:
Hi mates I want to know a simple program of return array from function ? Do I need to use pointer to return the address of the first element in an array. Isaac
4
by: Simon Schaap | last post by:
Hello, I have encountered a strange problem and I hope you can help me to understand it. What I want to do is to pass an array of chars to a function that will split it up (on every location where...
8
by: Peter B. Steiger | last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C is a simple word game that involves stringing together random lists of words. In the Pascal version the whole array was...
5
by: Stijn van Dongen | last post by:
A question about void*. I have a hash library where the hash create function accepts functions unsigned (*hash)(const void *a) int (*cmp) (const void *a, const void *b) The insert function...
8
by: Piotrek | last post by:
Hi, Like almost all of beginners I have problem understanding pointers. Please, look at this piece of code, and please explain me why myswap function doesn't work as it's supposed to do, whereas...
6
by: M Turo | last post by:
Hi, I was wondering if anyone can help. I'm want to pre-load a 'table' of function pointers that I can call using a its arrayed index, eg (non c code example) pFunc = func_A; pFunc = func_B;
2
by: StevenChiasson | last post by:
For the record, not a student, just someone attempting to learn C++. Anyway, the problem I'm having right now is the member function detAddress, of object controller. This is more or less, your...
5
by: Immortal Nephi | last post by:
I would like to design an object using class. How can this class contain 10 member functions. Put 10 member functions into member function pointer array. One member function uses switch to call...
33
by: Adam Chapman | last post by:
Hi, Im trying to migrate from programming in Matlab over to C. Im trying to make a simple function to multiply one matrix by the other. I've realised that C can't determine the size of a 2d...
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: 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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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,...
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.