473,395 Members | 1,452 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.

function pointer and pointer to function

Hi all,

Are function pointer and pointer to a function same ?

How do we declare a function pointer ?

How to declare an array of function pointers ?

Can you please give some examples for these.

Thanks,
Apr 10 '08 #1
4 1516
pr****************@gmail.com wrote:
Hi all,

Are function pointer and pointer to a function same ?
Yes.
How do we declare a function pointer ?
It depends on the type of function you want to point to. For example, to
declare fp as a pointer to a function taking char * and returning int,
you'd say:

int (*fp)(char *);

All parenthesis () are necessary. If you say:

int *fp(char *);

then you're declaring fp as a function taking 'char *' and returning
'int *'.
How to declare an array of function pointers ?
When things get more complicated, such as arrays of function pointers,
or pointers to functions taking pointers to functions and returning a
pointer to function, you can still declare them directly, but you can
also do it in stages using typedefs. There's a wonderful example in the
FAQ, question 1.21.

Philip
Apr 10 '08 #2
On Apr 10, 4:17*pm, Philip Potter <p...@doc.ic.ac.ukwrote:
prashant.khade1...@gmail.com wrote:
Hi all,
Are function pointer and pointer to a function same ?

Yes.
How do we declare a function pointer ?

It depends on the type of function you want to point to. For example, to
declare fp as a pointer to a function taking char * and returning int,
you'd say:

int (*fp)(char *);

All parenthesis () are necessary. If you say:

int *fp(char *);

then you're declaring fp as a function taking 'char *' and returning
'int *'.
How to declare an array of function pointers ?

When things get more complicated, such as arrays of function pointers,
or pointers to functions taking pointers to functions and returning a
pointer to function, you can still declare them directly, but you can
also do it in stages using typedefs. There's a wonderful example in the
FAQ, question 1.21.

Philip
thanks for your help...

I need to know about declaring array of function pointers and how to
use them
Apr 11 '08 #3
pr****************@gmail.com wrote:
On Apr 10, 4:17 pm, Philip Potter <p...@doc.ic.ac.ukwrote:
>prashant.khade1...@gmail.com wrote:
>>How to declare an array of function pointers ?
When things get more complicated, such as arrays of function pointers,
or pointers to functions taking pointers to functions and returning a
pointer to function, you can still declare them directly, but you can
also do it in stages using typedefs. There's a wonderful example in the
FAQ, question 1.21.

Philip

thanks for your help...

I need to know about declaring array of function pointers and how to
use them
I have already answered your question. I have quoted my answer above. If
you still don't understand, ask more specific questions - if you ask the
same question again, I can't tell which bit of my answer you haven't
understood.
Apr 11 '08 #4
Groovy hepcat Philip Potter was jivin' in comp.lang.c on Fri, 11 Apr
2008 5:30 pm. It's a cool scene! Dig it.
pr****************@gmail.com wrote:
>On Apr 10, 4:17 pm, Philip Potter <p...@doc.ic.ac.ukwrote:
>>prashant.khade1...@gmail.com wrote:
How to declare an array of function pointers ?
When things get more complicated, such as arrays of function
pointers, or pointers to functions taking pointers to functions and
returning a pointer to function, you can still declare them
directly, but you can also do it in stages using typedefs. There's a
wonderful example in the FAQ, question 1.21.

Philip

thanks for your help...

I need to know about declaring array of function pointers and how to
use them

I have already answered your question. I have quoted my answer above.
If you still don't understand, ask more specific questions - if you
ask the same question again, I can't tell which bit of my answer you
haven't understood.
True the OP *could* declare an array of pointer to function by using
typedef, but I think he wants to declare it directly. It's easy enough.
You haven't forgotten how to do it, have you, Phil? :)

int (*fpa[10])(int);

This declares an array of 10 pointers to function taking and int and
returning an int.

--
Dig the sig!

----------- Peter 'Shaggy' Haywood ------------
Ain't I'm a dawg!!
Jun 27 '08 #5

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...
37
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
2
by: sushil | last post by:
+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
27
by: Marlene Stebbins | last post by:
I am experimenting with function pointers. Unfortunately, my C book has nothing on function pointers as function parameters. I want to pass a pointer to ff() to f() with the result that f() prints...
23
by: bluejack | last post by:
Ahoy... before I go off scouring particular platforms for specialized answers, I thought I would see if there is a portable C answer to this question: I want a function pointer that, when...
3
by: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules'...
54
by: John | last post by:
Is the following program print the address of the function? void hello() { printf("hello\n"); } void main() { printf("hello function=%d\n", hello); }
26
by: aruna.mysore | last post by:
Hi all, I have a specific problem passing a function pointer array as a parameter to a function. I am trying to use a function which takes a function pointer array as an argument. I am too sure...
20
by: MikeC | last post by:
Folks, I've been playing with C programs for 25 years (not professionally - self-taught), and although I've used function pointers before, I've never got my head around them enough to be able to...
10
by: Richard Heathfield | last post by:
Stephen Sprunk said: <snip> Almost. A function name *is* a pointer-to-function. You can do two things with it - copy it (assign its value to an object of function pointer type, with a...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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
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
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...
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...

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.