What is the use of Function Pointers in C language?

S

sabarish

Hi friend,

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

Jack Klein

Hi friend,

what is the use of function pointer in c language and where it is
useful?

They are not useful to anyone who won't open a book or do a net
search.
tell with simple example...?
plz help me.

Here is a simple suggestion: open your C book and read it. If you
don't have a C book, get one.

You can also use your web browser. Go to www.google.com and type
"function pointer c", without the quotes. Click the search button.
Read.
 
M

Malcolm

sabarish said:
what is the use of function pointer in c language and where it is
useful?
To pass a function by indirection.
Or in simple English, tell a function what code to execute, instead of just
telling it what data to operate upon.
tell with simple example...?
plz help me.
Look up qsort in your C manual.
 
M

Markus.Elfring

A

Alvin Doowatz

Jack said:
They are not useful to anyone who won't open a book or do a net
search.




Here is a simple suggestion: open your C book and read it. If you
don't have a C book, get one.

You can also use your web browser. Go to www.google.com and type
"function pointer c", without the quotes. Click the search button.
Read.

I sort of had the same question.

The only C book I have is by an author named Herbert Schildt that I
bought used at a local bookstore. But I found his explanations somewhat
confusing. Is he condsidered a good author on C? I will trust you Jack
with your recommendation. Please tell me what C book I should get, if
not the one I have. The one I have is "The Annotated ANSI C Standard".

Please forgive me if I seem like a babe in the woods. I'm still trying
to figure out this whole Internet thing. And I am still trying to figure
out this whole C thing. Unlike possibly some of you out there, I wasn't
born with a (good) C book in my hand.

Thank you sir
 
K

Keith Thompson

Alvin Doowatz said:
The only C book I have is by an author named Herbert Schildt that I
bought used at a local bookstore. But I found his explanations
somewhat confusing. Is he condsidered a good author on C? I will trust
you Jack with your recommendation. Please tell me what C book I should
get, if not the one I have. The one I have is "The Annotated ANSI C
Standard".

Are you serious?

At the time that book was published, a copy of the ANSI/ISO C standard
cost $130.00. Schildt's book, which includes the standard plus
Schildt's annotations, was about $40. It's suggested that the
difference in price reflects the value of the annotations.

See <http://www.eskimo.com/~scs/C-faq/q11.2.html> for the relevant FAQ
entry, and <http://www.lysator.liu.se/c/schildt.html> for an
appropriately scathing review of the book.
 
J

John Bode

Alvin said:
I sort of had the same question.

The only C book I have is by an author named Herbert Schildt that I
bought used at a local bookstore. But I found his explanations somewhat
confusing. Is he condsidered a good author on C?

No. At least, he wasn't the last time I checked. His main problems
were that he didn't describe the language correctly, assumed that
everyone was programming on DOS or Windows (many of his examples used
DOS- and Windows-specific extensions that didn't work anywhere else),
taught some *bad* habits, and his examples often had bugs (I remember
one where he closed a stream and then attempted to write to it). I've
heard that his latest editions aren't so bad, but I wouldn't trust him
as a primary reference.
I will trust you Jack
with your recommendation. Please tell me what C book I should get, if
not the one I have. The one I have is "The Annotated ANSI C Standard".

It's not bad if you ignore the annotations.

Here's a list of reviews and recommendations for introductory C texts:

http://www.accu.org/bookreviews/public/reviews/0sb/beginner_s_c.htm

I personally use "C: A Reference Manual" by Harbison & Steele as my
primary reference. It is exactly what it says it is, a reference
manual; although it has some discussion and examples, it's not a good
introductory text. Kernighan and Ritchie's "The C Programming
Language" was long considered *the* introductory work, but my
understanding is that they aren't going to release a new edition to
reflect the latest (C99) standard.
 
R

Richard Heathfield

John Bode said:
Kernighan and Ritchie's "The C Programming
Language" was long considered *the* introductory work, but my
understanding is that they aren't going to release a new edition to
reflect the latest (C99) standard.

There's no point in doing so, since nobody seems to be interested in
producing a conforming C99 implementation - and even if they did, C99
programs still wouldn't be portable (unless that implementation were
portable - e.g. written in C90!).
 
D

Default User

Richard said:
John Bode said:


There's no point in doing so, since nobody seems to be interested in
producing a conforming C99 implementation - and even if they did, C99
programs still wouldn't be portable (unless that implementation were
portable - e.g. written in C90!).


I think Greg Comeau's got his pretty close.


Brian
 
J

Jordan Abel

John Bode said:


There's no point in doing so, since nobody seems to be interested in
producing a conforming C99 implementation - and even if they did, C99
programs still wouldn't be portable (unless that implementation were
portable - e.g. written in C90!).

Which would probably be possible in principle - imagine a c99
translator, written in c90, whose output is c90.

Or even one written in c99 - you could bootstrap by taking its own
output of itself to the platform where only a c90 compiler existed.
 
A

Alvin Doowatz

Keith said:
Are you serious?

At the time that book was published, a copy of the ANSI/ISO C standard
cost $130.00. Schildt's book, which includes the standard plus
Schildt's annotations, was about $40. It's suggested that the
difference in price reflects the value of the annotations.

See <http://www.eskimo.com/~scs/C-faq/q11.2.html> for the relevant FAQ
entry, and <http://www.lysator.liu.se/c/schildt.html> for an
appropriately scathing review of the book.

Thank you Keith:

After reading about Schildt at the link you provided, I realize I got a
good deal but should put blinders on with respect to his annotations?
That helps me a lot.

I also found the link to the FAQs extremely helpful, and read 99.9% of
it last night. I will try to refrain from asking questions not covered
by the FAQs, since that is something that this group disdains, it seems?

I am trying to learn C using Visual Studio C++, and appreciate your guys
and gals tolerance. I am a "newbie", so please forgive me if I make an
honest mistake. I appreciate all of your help. You guys and gals are
great. I want to learn C and become an expert like you guys and gals.

Thanks immensely
 
F

Flash Gordon

Alvin Doowatz wrote:

After reading about Schildt at the link you provided, I realize I got a
good deal but should put blinders on with respect to his annotations?
That helps me a lot.

Yes, read the standard ignore the annotations.
I also found the link to the FAQs extremely helpful, and read 99.9% of
it last night. I will try to refrain from asking questions not covered
by the FAQs, since that is something that this group disdains, it seems?

We talk about things not covered by the FAQ.

If you have a query about an item on the FAQ we will answer it, but
please state that it is a query on the FAQ so we don't just tell you ro
read it!

We do not talk about system specifics.

Question like, "is there a standard way to do X?" are fine even if the
answer is no, because you are asking about what can be done in standard C.

We will help with problems you have with standard C.

We don't write your programs for you.

There are better groups for discussion of general algorithms.
I am trying to learn C using Visual Studio C++, and appreciate your guys
and gals tolerance.

You need to make sure it is being invoked as a C compiler rather than a
C++ compiler (check the properties for the project). I recently took
over a project from a professional programmer, and telling VC++ that it
was C code rather than C++ drastically reduced the number of warnings.
> I am a "newbie", so please forgive me if I make an
honest mistake. I appreciate all of your help. You guys and gals are
great. I want to learn C and become an expert like you guys and gals.

We are here to help.
 
F

Flash Gordon

grid said:
There is an entire site on function pointers :
www.function-pointer.org

A quick look at that site suggests it should be treated with a degree of
caution.

It tells you to specify cdecl calling conventions on one page,
specifying how to do it with GNU GCC and something else, but fails to
mention that is highly Windows specific.

It talks about qsort, and shows a non-standard prototype for it.

It uses identifiers starting with an _ in at least one example (it may
be valid in that specific case, but it does not warn that in many
situations you can't use identifiers starting with an _).

It suggests that time.h provides a prototype for a function called
randomize.

Almost all of the examples are C++ rather than C.
 
J

Jordan Abel

A quick look at that site suggests it should be treated with a degree of
caution.

It tells you to specify cdecl calling conventions on one page,
specifying how to do it with GNU GCC and something else, but fails to
mention that is highly Windows specific.

Actually, it's i386-specific. [Which is almost as bad, but still]
 
H

hiddenharmony

"The C Programming Language" by Kernighan and Ritchie is a good
starting point for C language.

regards
VK
 
M

Michael Wojcik

It tells you to specify cdecl calling conventions on one page,
specifying how to do it with GNU GCC and something else, but fails to
mention that is highly Windows specific.

Actually, it's i386-specific. [Which is almost as bad, but still]

"cdecl" (and "_cdecl", "__cdecl", "__stdcall", etc), as the name of a
"calling convention", is a Microsoft extension which is supported for
x86 processors other than the i386, including at least some of the
IA-16 processors that preceeded the 386 and the various IA-32 and now
AMD64 / EMT processors that succeeded it.[1]

It was also supported on non-x86 processors back when Microsoft
shipped Windows for non-x86 systems (other than Itanium).[2] The
specifics of register use in the various calling conventions differed
for non-x86 processors, of course, but the calling-convention
keywords were still supported, and the convention specified by
__cdecl still differed from that specified by __stdcall.

GCC happens to describe its "cdecl" and "stdcall" attribute
extensions as 386-specific,[3] but the GCC documentation is not a
definitive history of how and where particular extensions to C have
been used.

The "cdecl calling convention" is an extension provided by certain
implementations. It is incorrect to claim that it is specific to
Microsoft Windows (though that is where it is predominantly found) or
to the i386 (despite poor wording in the GCC documentation).


1. See for example
http://blogs.msdn.com/oldnewthing/archive/2004/01/02/47184.aspx.

2. See for example "/Gd, /Gr, /Gz (Calling Convention)" in the
October 2001 MSDN Library Collection.

3. http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html#Function Attributes

--
Michael Wojcik (e-mail address removed)

The antics which have been drawn together in this book are huddled here
for mutual protection like sheep. If they had half a wit apiece each
would bound off in many directions, to unsimplify the target. -- Walt Kelly
 
J

Jordan Abel

Jordan Abel said:
Actually, it's i386-specific. [Which is almost as bad, but still]

"cdecl" (and "_cdecl", "__cdecl", "__stdcall", etc), as the name of a
"calling convention", is a Microsoft extension

Also gcc. By the way, cdecl is the default calling convention
which is supported for x86 processors other than the i386, including
at least some of the IA-16 processors that preceeded the 386 and the
various IA-32 and now AMD64 / EMT processors that succeeded it.[1]

"i386" as used in my post means the entire family. Sorry if that wasn't
clear
It was also supported on non-x86 processors back when Microsoft
shipped Windows for non-x86 systems (other than Itanium).[2] The
specifics of register use in the various calling conventions differed
for non-x86 processors,

Those two calling conventions [on x86, anyway] have little to do with
register use - it's a matter of who cleans up the stack. on cdecl, it's
the caller, on stdcall, it's the callee.
 
M

Michael Wojcik

Jordan Abel said:
Actually, it's i386-specific. [Which is almost as bad, but still]

"cdecl" (and "_cdecl", "__cdecl", "__stdcall", etc), as the name of a
"calling convention", is a Microsoft extension

Also gcc.

I wasn't clear: it's an extension created by Microsoft and later
adopted by gcc.
which is supported for x86 processors other than the i386, including
at least some of the IA-16 processors that preceeded the 386 and the
various IA-32 and now AMD64 / EMT processors that succeeded it.[1]

"i386" as used in my post means the entire family. Sorry if that wasn't
clear

Fair enough. Generally when I see "i386" it means either the Intel
80386 only, or the 386 and its direct successors (sometimes including
the 64-bit extended CPUs). I wanted to emphasize that "cdecl"
preceeded the 32-bit processors in the x86 family.
It was also supported on non-x86 processors back when Microsoft
shipped Windows for non-x86 systems (other than Itanium).[2] The
specifics of register use in the various calling conventions differed
for non-x86 processors,

Those two calling conventions [on x86, anyway] have little to do with
register use - it's a matter of who cleans up the stack. on cdecl, it's
the caller, on stdcall, it's the callee.

No, the Microsoft calling-convention extensions specify register use
as well, for the x86 family. See the MSDN article I cited in my
previous post; it describes specific differences in register use for
the calling conventions on x86. What I wrote above is completely
accurate: the calling conventions were defined by Microsoft for non-
x86 processors, but the x86-specific register use portions of the
definitions did not, of course, apply to those processors.

Your parenthetical comment is exactly wrong: on x86, they *did* have
something to do with register use.

gcc may not include any register-use requirements in its specification
of the calling conventions, but Microsoft did, just as I stated.

--
Michael Wojcik (e-mail address removed)

Thus, the black lie, issuing from his base throat, becomes a boomerang to
his hand, and he is hoist by his own petard, and finds himself a marked man.
-- attributed to a "small-town newspaper editor in Wisconsin"
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top