What is the use of Function Pointers in C language?

T

tnowles00

Hi friend,

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

Ian Collins

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.
 
U

user923005

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.
 
I

Ian Collins

user923005 said:
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.
 
P

p_cricket_guy

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.
 
K

Keith Thompson

Ian Collins said:
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.
 
R

Richard Heathfield

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.)
 
F

Flash Gordon


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.
 
K

Keith Thompson

Richard Heathfield said:
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.
 
R

Richard Heathfield

Keith Thompson said:
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!
 
S

santosh

Richard said:
Keith Thompson said:


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.


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.
 
T

tehn.yit.chin

Keith Thompson said:





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
 
S

santosh

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.
 
R

Richard Heathfield

(e-mail address removed) said:
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.
 
R

Richard Heathfield

santosh said:
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>
 
S

santosh

Richard said:
santosh said:


I don't. I stand by my statement.


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.
 
C

CBFalconer

.... 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top