function prototype vs function declaration

P

Philip Potter

santosh said:
phao said:
Hi, i was reading this post and i'd like to know if is wrong doing
this:

/* begin of code */
datatype func_name(arglist) { ... } [snip]

suposing datatype is a valid datatype and arglist is a list of
arguments with valid datatypes and names, and func_name is a valid
name and { ... } for each function is a valid block of code.
>
Your compiler should emit a diagnostic for calling a function with no
prototype in scope. If it does not, please read your compiler's
documentation and supply the appropriate switches to make it do so.
Almost all compilers can diagnose this code feature.

I don't see why or where this diagnostic should arise. From his
definition of 'arglist' then he is using prototypes rather than K&R
declarations, and nothing from his post suggests he's calling func_name
before declaring it. I believe he was asking the difference between
defining all functions before use, with main() the last defined
function, and declaring all functions before use but defining them in
any order (say, with main() defined first).
 
P

pete

Chris said:
pete wrote:
[snip]
But Pete's observation is
correct if interpreted one way, since "pointer to incomplete type"
is itself an "object type".

Pointer to function type is also an object type,
but that's not the point.
(I am not sure what he meant by the phrase "object pointers" though.)

That's the point.
The term "object pointers" appears both in K&R2
and also in the C standard.
What does it mean?

Incomplete types and object types
are both defined as describing objects.
If "object pointers" meant collectively
both pointers to object types and pointers to incomplete types,
then both K&R2 and the title of section 7.18.1.4 of the C99 standard,
would make more sense than they do now.

The point that I made, that you were examining,
is that the difference between:
a pointer declared as a pointer to an incomplete type,
and a pointer to an object type
can be just as small as it being the same pointer
at a different phase of program translation.

Pointers to function types on the other hand,
are very different from pointers to object types,
and so I would classify pointers into two major groups:
1 function pointers
2 object pointers
(pointers to incomplete types and
pointers to object types)
Here's the real real problem.
I think that I read that somewhere and that I didn't make it up,
but I have no references,
and nobody else knows what I'm talking about.
So, I have to consider that I may be wrong and that
the references may just have been a dream.
 
H

h03Ein

Ravishankar S said:

<snip>





I think you should stop writing tutorials and start reading them. There is
a very important distinction between a declaration and a prototype, and
you do your readers a disservice by not making the distinction clear.

What's more, there are (rather recondite) circumstances where you could
conceivably need to eschew the prototype form - I'm thinking particularly
of arrays of pointer to function where not all the functions have the same
type. It is important to know when to use the prototype form (i.e. nearly
always) and when it *might* be a good idea not to.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999

Here is a textbook note:
"Subprogram declarations are common in C programs, where they are
called prototypes."

textbook : <Concepts of Programming Languages
Robert W. Sebesta
University of Colorado, Colorado Springs
Addison&Wisely> 5th edition
chapter 9 (Subprograms) 9.2.2 Basic definition.
 
J

James Kuyper

h03Ein said:
....
Here is a textbook note:
"Subprogram declarations are common in C programs, where they are
called prototypes."

I'm not sure why you cited that; it's true only insofar as it's a
simplification that ignores the fact that in C, some function
declarations are not prototypes. If the author meant that not as a
simplification, but as a definitive statement that there are no
non-prototype function declarations in C, you should identify the
textbook - it should be avoided.
 
F

Flash Gordon

h03Ein wrote, On 23/12/07 06:53:
Please don't quote peoples signatures, the bit typically after the "-- "
unless you are commenting on them.
Here is a textbook note:
"Subprogram declarations are common in C programs, where they are
called prototypes."

textbook : <Concepts of Programming Languages
Robert W. Sebesta
University of Colorado, Colorado Springs
Addison&Wisely> 5th edition
chapter 9 (Subprograms) 9.2.2 Basic definition.

Text books do not trump the standard, and the standard explicitly allows
functions declarations that are *not* prototypes. Specifically in
section 6.2 paragraph 2 of N1256 it states "A function /prototype/ is a
declaration of a function that declares the types of its parameters."
thus making it clear that prototypes are a subset of functions declarations.
 

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,744
Messages
2,569,479
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top