By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,303 Members | 1,590 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,303 IT Pros & Developers. It's quick & easy.

What is the use of Function Pointers in C language?

P: n/a
Hi friend,

what is the use of function pointer in c language and where it is
useful?
tell with simple example...?
plz help me.

Feb 21 '07 #1
Share this Question
Share on Google+
26 Replies


P: n/a
tn*******@gmail.com wrote:
Hi friend,

what is the use of function pointer in c language and where it is
useful?
tell with simple example...?
plz help me.
plz doesn't post here any more.

Functions pointers have many uses. They are used to implement generic
functions like qsort(), callbacks, state machines and anywhere else run
time binding of a function is required.

--
Ian Collins.
Feb 21 '07 #2

P: n/a
On Feb 20, 8:04 pm, tnowle...@gmail.com wrote:
Hi friend,

what is the use of function pointer in c language and where it is
useful?
tell with simple example...?
plz help me.

In sorting, we can pass a comparison function to a sort routine and
have a completely general sort for any data type. See:
http://www.hmug.org/man/3/qsort.php

Feb 21 '07 #3

P: n/a
On Feb 20, 8:19 pm, Ian Collins <ian-n...@hotmail.comwrote:
tnowle...@gmail.com wrote:
Hi friend,
what is the use of function pointer in c language and where it is
useful?
tell with simple example...?
plz help me.

plz doesn't post here any more.
How's he going to learn then?

Rather than hoping that posters go away, I hope that they will
improve. Sometimes they do.
Functions pointers have many uses. They are used to implement generic
functions like qsort(), callbacks, state machines and anywhere else run
time binding of a function is required.

--
Ian Collins.

Feb 21 '07 #4

P: n/a
user923005 wrote:
On Feb 20, 8:19 pm, Ian Collins <ian-n...@hotmail.comwrote:
>>tnowle...@gmail.com wrote:
>>>Hi friend,
>>>what is the use of function pointer in c language and where it is
useful?
tell with simple example...?
plz help me.

plz doesn't post here any more.

How's he going to learn then?

Rather than hoping that posters go away, I hope that they will
improve. Sometimes they do.
Read what I wrote again. I said the the mysterious 'plz' doesn't post
here any more, I didn't say anything to the OP about going away.

--
Ian Collins.
Feb 21 '07 #5

P: n/a
On Feb 21, 9:04 am, tnowle...@gmail.com wrote:
Hi friend,

what is the use of function pointer in c language and where it is
useful?
tell with simple example...?
plz help me.


Pointer to function is useful when you want to choose a
function dynamically at run time - usually based on certain
conditions.

To begin, check out the qsort fucntion in <stdlib.h>:

void
qsort(void *base, size_t nmemb, size_t size,
int (*compar)(const void *, const void *));

Here, you need to pass a pointer to the function that
compares the objects that you are sorting. Since qsort()
is a generic implementation and does not care what you
are sorting, it is up to you to implement a funtion
which compares the objects that you are sorting and
pass it on to qsort(). qsort() just invokes your
compare function by dereferencing the function
pointer.
Feb 21 '07 #6

P: n/a
Ian Collins <ia******@hotmail.comwrites:
user923005 wrote:
>On Feb 20, 8:19 pm, Ian Collins <ian-n...@hotmail.comwrote:
>>>tnowle...@gmail.com wrote:

Hi friend,

what is the use of function pointer in c language and where it is
useful?
tell with simple example...?
plz help me.

plz doesn't post here any more.

How's he going to learn then?

Rather than hoping that posters go away, I hope that they will
improve. Sometimes they do.
Read what I wrote again. I said the the mysterious 'plz' doesn't post
here any more, I didn't say anything to the OP about going away.
But if the OP doesn't know any better than to write "plz", he's likely
to miss the joke.

To the original poster: Please don't use silly abbreviations like
"plz", "u", "ur", and so forth. If you want us to take the time to
read what you've written, you need to take the time to spell out the
words. This is not a chat room.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 21 '07 #7

P: n/a
On Feb 21, 9:04 am, tnowle...@gmail.com wrote:
Hi friend,

what is the use of function pointer in c language and where it is
useful?
tell with simple example...?
plz help me.
this are good resource

www.function-pointer.org/
en.wikipedia.org/wiki/Function_pointer

--Raxit

Feb 21 '07 #8

P: n/a
Keith Thompson said:
Ian Collins <ia******@hotmail.comwrites:
>user923005 wrote:
>>On Feb 20, 8:19 pm, Ian Collins <ian-n...@hotmail.comwrote:
tnowle...@gmail.com wrote:
>plz help me.

plz doesn't post here any more.

How's he going to learn then?

Rather than hoping that posters go away, I hope that they will
improve. Sometimes they do.
Read what I wrote again. I said the the mysterious 'plz' doesn't
post here any more, I didn't say anything to the OP about going away.

But if the OP doesn't know any better than to write "plz", he's likely
to miss the joke.
If the OP doesn't know any better than to write "plz", he's unlikely to
be bright enough to learn C. (Yes, it *could* happen - it's just
unlikely.)

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Feb 21 '07 #9

P: n/a
ra************@yahoo.co.in wrote, On 21/02/07 09:56:
On Feb 21, 9:04 am, tnowle...@gmail.com wrote:
>Hi friend,

what is the use of function pointer in c language and where it is
useful?
tell with simple example...?
plz help me.
this are good resource

www.function-pointer.org/
This paragraph is incorrect:
| Both, the executable compiled program code and the used variables, are
| put inside this memory. Thus a function in the program code is, like
| e.g. a character field, nothing else than an address. It is only
| important how you, or better your compiler/processor, interpret the
| memory a pointer points to.

I have developed in C for processors where program and address space
were completely different. On the implementation I am thinking of
unsigned int is large enough to represent and function or object
pointer, so on this implementation is is entirely possible you could
find "(unsigned int)func_ptr == (unsigned int)object_ptr" was true.

It also erroneously states, "Note: Although you may ommit the address
operator on most compilers you should always use the correct way in
order to write portable code" when in actual fact the standard allows
you to omit the & operator so on *any* conforming implementation you do
not need it.
en.wikipedia.org/wiki/Function_pointer
This isn't since the sentence "Function pointers are of the type the
function returns." is incorrect. Function pointers are of type pointer
to function (actually, they point to a function with with the specified
signature).
So over all, no, those are *not* good resources.
--
Flash Gordon
Feb 21 '07 #10

P: n/a
Richard Heathfield <rj*@see.sig.invalidwrites:
[...]
If the OP doesn't know any better than to write "plz", he's unlikely to
be bright enough to learn C. (Yes, it *could* happen - it's just
unlikely.)
I think that's likely to be unfair. There are contexts in which those
silly little abbreviations like "plz", "u", and "ur" are considered
appropriate (chat rooms, text messaging); given the awkwardness of
entering text on a mobile phone keypad, it's quite understandable.
And it's not entirely unreasonable (though it's wrong) to assume that
since Usenet is another computer-based discussion forum, the same
rules might apply.

Posting to a forum without first looking around to see what the
conventions are is a mistake, but most posters make that mistake only
once; there are just enough new posters showing up to make it look
like a continuous major influx. See
<http://en.wikipedia.org/wiki/Eternal_September>; thus today is
September 4922, 1993.

We have seen people learn and improve. They just need to remember
that using "plz" for "please" is bad, but using "char" for
"character", "{" for "begin", and "" for "function" are mandatory.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 21 '07 #11

P: n/a
Keith Thompson said:
Richard Heathfield <rj*@see.sig.invalidwrites:
[...]
>If the OP doesn't know any better than to write "plz", he's unlikely
to be bright enough to learn C. (Yes, it *could* happen - it's just
unlikely.)

I think that's likely to be unfair. There are contexts in which those
silly little abbreviations like "plz", "u", and "ur" are considered
appropriate (chat rooms, text messaging);
By whom? In IRC, I will quickly /ignore anyone inconsiderate enough to
use such abbreviations. If I get a text message that I can't read, I
don't bother trying to decode it. I just delete it. If the sender later
asks whether I got their message, I reply (truthfully) that I got an
obviously corrupted message which I deleted unread.
given the awkwardness of
entering text on a mobile phone keypad, it's quite understandable.
If I can write English on mobile phone keypads, so can other
English-speaking people. It's hardly difficult.
And it's not entirely unreasonable (though it's wrong) to assume that
since Usenet is another computer-based discussion forum, the same
rules might apply.
The same rules DO apply!

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Feb 21 '07 #12

P: n/a
Richard Heathfield wrote:
Keith Thompson said:
Richard Heathfield <rj*@see.sig.invalidwrites:
[...]
If the OP doesn't know any better than to write "plz", he's unlikely
to be bright enough to learn C. (Yes, it *could* happen - it's just
unlikely.)
I think that's likely to be unfair. There are contexts in which those
silly little abbreviations like "plz", "u", and "ur" are considered
appropriate (chat rooms, text messaging);

By whom? In IRC, I will quickly /ignore anyone inconsiderate enough to
use such abbreviations. If I get a text message that I can't read, I
don't bother trying to decode it. I just delete it. If the sender later
asks whether I got their message, I reply (truthfully) that I got an
obviously corrupted message which I deleted unread.
given the awkwardness of
entering text on a mobile phone keypad, it's quite understandable.

If I can write English on mobile phone keypads, so can other
English-speaking people. It's hardly difficult.
If I do send text messages, (quite rare), I always take the time to
type the message in correct English. SMS-speak is horrible beyond
words. It's mainly acquired from folks who send a zillion text
messages a day, something that's abnormal in and of itself, and like
all habits, it quickly starts to stick to you.

Feb 22 '07 #13

P: n/a
On Feb 21, 10:16 pm, Richard Heathfield <r...@see.sig.invalidwrote:
Keith Thompson said:
Ian Collins <ian-n...@hotmail.comwrites:
user923005 wrote:
On Feb 20, 8:19 pm, Ian Collins <ian-n...@hotmail.comwrote:
tnowle...@gmail.com wrote:
plz help me.
>>>plz doesn't post here any more.
>How's he going to learn then?
>Rather than hoping that posters go away, I hope that they will
improve. Sometimes they do.
Read what I wrote again. I said the the mysterious 'plz' doesn't
post here any more, I didn't say anything to the OP about going away.
But if the OP doesn't know any better than to write "plz", he's likely
to miss the joke.

If the OP doesn't know any better than to write "plz", he's unlikely to
be bright enough to learn C. (Yes, it *could* happen - it's just
unlikely.)
That is really unfair. I mean, the whole point of writing a post is to
pass on a message. Some of us reading the message understands what
"plz" means, and provide some answers for it. Posting a comment which
is negative and personal like un-called for.

I wouldn't be surprise if "plz" becomes mainstream in a few
generations.

Tehn Yit Chin

Feb 22 '07 #14

P: n/a
te***********@gmail.com wrote:
On Feb 21, 10:16 pm, Richard Heathfield <r...@see.sig.invalidwrote:
<snip>
If the OP doesn't know any better than to write "plz", he's unlikely to
be bright enough to learn C. (Yes, it *could* happen - it's just
unlikely.)

That is really unfair. I mean, the whole point of writing a post is to
pass on a message. Some of us reading the message understands what
"plz" means, and provide some answers for it. Posting a comment which
is negative and personal like un-called for.
Apart from the aspersions cast on the OP's ability to learn C, (which
I agree was in bad taste), the main point of the thread stands: that
using silly abbreviations is inappropriate and counterproductive.
I wouldn't be surprise if "plz" becomes mainstream in a few
generations.
Curiously, this sort of mirrors the "standard vs. non-standard"
discussions that often take place here. The letter sequence 'plz' is a
non-standard but in some circles common form for the standard English
word 'please'. Using non-standard variants can potentially be
confusing because someone else might use 'pls' or 'pleaz' or 'pleas'
or 'plez' or 'ples' or another variant. Suddenly one well known and
well defined word has been burdened with innumerable poorly understood
and distasteful aliases.

Imagine the same happenning to most of the other commonly used words
and you begin to get the idea of the level of chaos that might result.

Though coining of new words is good, as is the development of a
programming language, in most cases it's better to stick to known and
accepted forms.

Feb 22 '07 #15

P: n/a
te***********@gmail.com wrote:
I wouldn't be surprise if "plz" becomes mainstream in a few
generations.

Tehn Yit Chin
Do you really, thnytch?

Richard
Feb 22 '07 #16

P: n/a
te***********@gmail.com said:
On Feb 21, 10:16 pm, Richard Heathfield <r...@see.sig.invalidwrote:
>Keith Thompson said:
But if the OP doesn't know any better than to write "plz", he's
likely to miss the joke.

If the OP doesn't know any better than to write "plz", he's unlikely
to be bright enough to learn C. (Yes, it *could* happen - it's just
unlikely.)

That is really unfair.
No, it isn't. It's common sense. Anyone asking other people for help in
C programming should be bright enough to realise that he will maximise
his chances of getting good quality help by minimising the time that
sometimes very busy people have to spend helping him. Writing his
message in some kind of obscure patois is therefore not a very
intelligent thing to do.
I mean, the whole point of writing a post is to pass on a message.
Yes, and the one to whom the communication is of some importance should
choose a communication form that will succeed in being understood
easily by the one to whom the communication isn't so important.
I wouldn't be surprise if "plz" becomes mainstream in a few
generations.
I wouldn't be surprised if wearing a ring through one's nose becomes
mainstream in a few years. That doesn't mean I'd choose to do it, or
find it attractive in those who do.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Feb 22 '07 #17

P: n/a
santosh said:
te***********@gmail.com wrote:
>On Feb 21, 10:16 pm, Richard Heathfield <r...@see.sig.invalidwrote:
<snip>
If the OP doesn't know any better than to write "plz", he's
unlikely to be bright enough to learn C. (Yes, it *could* happen -
it's just unlikely.)

That is really unfair. I mean, the whole point of writing a post is
to pass on a message. Some of us reading the message understands what
"plz" means, and provide some answers for it. Posting a comment which
is negative and personal like un-called for.

Apart from the aspersions cast on the OP's ability to learn C, (which
I agree was in bad taste),
I don't. I stand by my statement.
the main point of the thread stands: that
using silly abbreviations is inappropriate and counterproductive.
Right, and people who indulge in inappropriate, counterproductive
behaviour when supposedly learning a difficult technical subject are
less likely to succeed than those who do not.

<snip>

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Feb 22 '07 #18

P: n/a
Richard Heathfield wrote:
santosh said:
te***********@gmail.com wrote:
On Feb 21, 10:16 pm, Richard Heathfield <r...@see.sig.invalidwrote:
<snip>
If the OP doesn't know any better than to write "plz", he's
unlikely to be bright enough to learn C. (Yes, it *could* happen -
it's just unlikely.)

That is really unfair.
<snip>
Apart from the aspersions cast on the OP's ability to learn C, (which
I agree was in bad taste),

I don't. I stand by my statement.
the main point of the thread stands: that
using silly abbreviations is inappropriate and counterproductive.

Right, and people who indulge in inappropriate, counterproductive
behaviour when supposedly learning a difficult technical subject are
less likely to succeed than those who do not.
It's a bad habit and it's condoned in most chat rooms and web forums.
It doesn't follow that the concerned person lacks the intelligence to
learn a technical subject.

If the OP persists in using such aberrant words, even after being told
off, then he, (or she), is either truly rather dimwitted or is
malicious, with both of the said traits definitely not auguring well
for the OP's chances of successfully learning C.

Feb 22 '07 #19

P: n/a
te***********@gmail.com wrote:
>
.... snip ...
>
That is really unfair. I mean, the whole point of writing a post
is to pass on a message. ... snip ...

I wouldn't be surprise if "plz" becomes mainstream in a few
generations.
Exactly, and no. Failure to spell out the word fouls that message
and introduces unneeded ambiguity. Even if you only type at 60
wpm, the added time involved is 1/2 second. Even a hunt & pecker
can probably hit at least 15 wpm, or 2 full seconds.

You might as well recommend stuffing all ints into signed chars.
Same thing.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Feb 22 '07 #20

P: n/a
In article <11**********************@a75g2000cwd.googlegroups .com>,
<te***********@gmail.comwrote:
>I wouldn't be surprise if "plz" becomes mainstream in a few
generations.
The idea that we'll still be pressing keys to send messages in a few
generations seems most implausible.

-- Richard

--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Feb 22 '07 #21

P: n/a
On Feb 22, 7:03 pm, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:
tehn.yit.c...@gmail.com wrote:
I wouldn't be surprise if "plz" becomes mainstream in a few
generations.
Tehn Yit Chin

Do you really, thnytch?

Richard
Why not, the alphabet could have lost its letters, mostly its vowels,
and the "art" of using spaces to delimit words is lost, so my name
would become "thnytch". Your name could become rchrdbs.

cheers,
thnytch
Feb 22 '07 #22

P: n/a
te***********@gmail.com said:
On Feb 22, 7:03 pm, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:
>tehn.yit.c...@gmail.com wrote:
I wouldn't be surprise if "plz" becomes mainstream in a few
generations.
Tehn Yit Chin

Do you really, thnytch?

Richard

Why not, the alphabet could have lost its letters, mostly its vowels,
and the "art" of using spaces to delimit words is lost, so my name
would become "thnytch". Your name could become rchrdbs.
whystptvwlspprcslttrsspcsndpncttnrclrlysprfls

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Feb 22 '07 #23

P: n/a
te***********@gmail.com wrote:
On Feb 22, 7:03 pm, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:
tehn.yit.c...@gmail.com wrote:
I wouldn't be surprise if "plz" becomes mainstream in a few
generations.
Tehn Yit Chin
Do you really, thnytch?

Richard

Why not, the alphabet could have lost its letters, mostly its vowels,
and the "art" of using spaces to delimit words is lost, so my name
would become "thnytch". Your name could become rchrdbs.
Yes, don't worry, it'll certainly happen at the End of days. Till then
let better sense prevail!

Feb 22 '07 #24

P: n/a
te***********@gmail.com wrote:
>
.... snip ...
>
Why not, the alphabet could have lost its letters, mostly its
vowels, and the "art" of using spaces to delimit words is lost, so
my name would become "thnytch". Your name could become rchrdbs.
You misspelled "thnytchn" :-)

Thin yeti chino. (May illustrate the problem)

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Feb 23 '07 #25

P: n/a
te***********@gmail.com wrote:
On Feb 22, 7:03 pm, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:
tehn.yit.c...@gmail.com wrote:
I wouldn't be surprise if "plz" becomes mainstream in a few
generations.
Tehn Yit Chin
Do you really, thnytch?

Richard

Why not, the alphabet could have lost its letters, mostly its vowels,
and the "art" of using spaces to delimit words is lost, so my name
would become "thnytch". Your name could become rchrdbs.
Like hell it could.

Richard
Feb 23 '07 #26

P: n/a
On Feb 23, 12:26 pm, CBFalconer <cbfalco...@yahoo.comwrote:
tehn.yit.c...@gmail.com wrote:

... snip ...
Why not, the alphabet could have lost its letters, mostly its
vowels, and the "art" of using spaces to delimit words is lost, so
my name would become "thnytch". Your name could become rchrdbs.

You misspelled "thnytchn" :-)

Thin yeti chino. (May illustrate the problem)

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
First time in a long while anyone has call me thin ;-)

Feb 23 '07 #27

This discussion thread is closed

Replies have been disabled for this discussion.