Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Pointer to function in K&R C

Reply
Thread Tools

Pointer to function in K&R C

 
 
Ben Bacarisse
Guest
Posts: n/a
 
      09-09-2011
Keith Thompson <(E-Mail Removed)> writes:

> nathan <(E-Mail Removed)> writes:
> [...]
>> I did some Googling last night and just didn't get the answer so I
>> mixed thing up:
>>
>> The question is:
>>
>> int foo (x, y)
>> int x;
>> int y;
>> {}
>>
>> int bar (z)
>> int (*z) ();
>> {}

>
> That's correct for K&R C.
>
>> is this correct or do we need:
>>
>> int (*z) (int, int);

>
> That's a prototype (a function declaration that includes information
> about the types of the arguments),


To be picky, this is not a function declaration. It declares an
identifier that denotes an object type rather than a function.

As to whether it is a prototype, on the one hand section 6.2.1 p2
defines a /function prototype/ (in italics) as "a declaration of a
function that declares the types of its parameters". Since this does
not declare a function, it's out. On the other hand, section 6.5.2.2 p2
talks about an expression that denotes a called function (like the 'z'
in 'z(1,2)') as having "a type that includes a prototype". I take that
to mean that the above is not a function prototype but that it declares
an object with a type that includes a prototype.

<snip>
>> int (*z) (x, y);

>
> I don't think that's not valid syntax in any version of the language
> (unless x and y are type names).


There's an unintended "not" I think.

<snip>
--
Ben.
 
Reply With Quote
 
 
 
 
James Kuyper
Guest
Posts: n/a
 
      09-09-2011
On 09/08/2011 02:23 PM, Chad wrote:
> On Sep 7, 2:04 pm, Harald van Dijk <(E-Mail Removed)> wrote:
>> On Sep 7, 10:54 pm, nathan <(E-Mail Removed)> wrote:
>>
>>> I write C based software that I try to keep as portable as possible.
>>> Therefore I support both K&R and ANSI headers.

....
>>> In one of these
>>> headers, there is a pointer to a function with arguments. In ANSI
>>> that's easy, but how about K&R C? Is it:

>>
>> K&R C does not have "function with argument" types. It has function
>> types.
>>

>
> I don't see why the distinction is important.


K&R's function types were completely specified by the return type of the
function. The "function with argument" or prototype declarations that
were added in ANSI C also specified the number and the types of the
arguments (I won't bother trying to describe how functions with a
variable number of arguments complicate the description). Two prototype
declarations are compatible only if the number of arguments match and
the corresponding argument types are compatible.

When you call a function that is declared with a prototype, it's a
constraint violation to pass it the wrong number of arguments. Argument
values are assigned to the corresponding parameters, if any such
assignment would not be allowed, it is also a constraint violation. This
means you generally find out at compile time if you call a function with
the wrong number or wrong types of arguments.

For functions declared with K&R syntax, there's nothing to be checked,
and you won't find out that you've made such errors until run-time (and
not necessarily then, either, depending upon precisely how the function
call actually malfunctions).
--
James Kuyper
 
Reply With Quote
 
 
 
 
Jorgen Grahn
Guest
Posts: n/a
 
      09-09-2011
On Thu, 2011-09-08, Keith Thompson wrote:
> Jorgen Grahn <(E-Mail Removed)> writes:
>> On Wed, 2011-09-07, christian.bau wrote:
>> ...
>>> Of course any undefined behaviour in your program could lead to your
>>> code being transported 30 years back in time, where K&R compatibility
>>> could then come useful.

>>
>> That's why I include this in every program I write:
>>
>> if(time(0)<1e9) {
>> FILE* f = popen("/bin/mail http://www.velocityreviews.com/forums/(E-Mail Removed)", "w");
>> fputs(stock_market_of_1992, f);
>> fclose(f);
>> }
>>
>> I'm not rich yet. I guess that means all my programs work.

>
> You're not rich because you're sending 1992 stock market information
> back to 2001. ((time_t)1e9 is Sun 2001-09-09 01:46:40 UTC, assuming
> Unix-style time_t).


Ah, thanks. I actually *did* the math, but messed up on the
requirements. (And late 2001 was probably not the best time to make
money on stocks anyways; the IT bubble burst some months later as I
recall it.)

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
 
Reply With Quote
 
Jorgen Grahn
Guest
Posts: n/a
 
      09-09-2011
On Thu, 2011-09-08, Keith Thompson wrote:
> nathan <(E-Mail Removed)> writes:

....

>> I hope you catch my drift now. Yes, it's to maintain compatibility
>> with an old system (Coherent).

>
> Does that system not have a C compiler that supports prototypes?
> Can you build gcc for it (probably an older version)?


Googling on "gcc Coherent" shows among other things
http://www.coherentos.info/cohs-gcc-usefulness
You can buy (?) GCC for Coherent from someone named "MWC"
apparently.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      09-09-2011
On 09/09/2011 05:25 AM, Jorgen Grahn wrote:
> On Thu, 2011-09-08, Keith Thompson wrote:

....
>>
>> You're not rich because you're sending 1992 stock market information
>> back to 2001. ((time_t)1e9 is Sun 2001-09-09 01:46:40 UTC, assuming
>> Unix-style time_t).

>
> Ah, thanks. I actually *did* the math, but messed up on the
> requirements. (And late 2001 was probably not the best time to make
> money on stocks anyways; the IT bubble burst some months later as I
> recall it.)


Which would have made it an excellent time to sell short. If you know
what the market's going to do, you can almost always make money off of
your knowledge.
--
James Kuyper
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      09-09-2011
Ben Bacarisse <(E-Mail Removed)> writes:
> Keith Thompson <(E-Mail Removed)> writes:
>> nathan <(E-Mail Removed)> writes:

[...]
>>> int (*z) (x, y);

>>
>> I don't think that's not valid syntax in any version of the language
>> (unless x and y are type names).

>
> There's an unintended "not" I think.
>
> <snip>


Yes, thanks.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Tim Rentsch
Guest
Posts: n/a
 
      01-24-2012
Ben Bacarisse <(E-Mail Removed)> writes:

> Keith Thompson <(E-Mail Removed)> writes:
>
>> nathan <(E-Mail Removed)> writes:
>> [...]
>>> I did some Googling last night and just didn't get the answer so I
>>> mixed thing up:
>>>
>>> The question is:
>>>
>>> int foo (x, y)
>>> int x;
>>> int y;
>>> {}
>>>
>>> int bar (z)
>>> int (*z) ();
>>> {}

>>
>> That's correct for K&R C.
>>
>>> is this correct or do we need:
>>>
>>> int (*z) (int, int);

>>
>> That's a prototype (a function declaration that includes information
>> about the types of the arguments),

>
> To be picky, this is not a function declaration. It declares an
> identifier that denotes an object type rather than a function.
>
> As to whether it is a prototype, on the one hand section 6.2.1 p2
> defines a /function prototype/ (in italics) as "a declaration of a
> function that declares the types of its parameters". Since this does
> not declare a function, it's out. On the other hand, section 6.5.2.2 p2
> talks about an expression that denotes a called function (like the 'z'
> in 'z(1,2)') as having "a type that includes a prototype". I take that
> to mean that the above is not a function prototype but that it declares
> an object with a type that includes a prototype. [snip unrelated]


That's simply bad wording in this definition; presumably what was
meant is 'function declarator'. Clearly the Standard intends the
term 'function prototype' to include things like declarations of
pointer-to-function types, otherwise other passages in the Standard
are nonsensical -- cf. 6.2.1 p4. I agree the wording in 6.2.1p2 is
poorly chosen, but surely the intended meaning is clear.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
function pointer and pointer to function prashant.khade1623@gmail.com C Programming 3 04-11-2008 07:30 AM
diffrence between "function pointer" and "pointer to a function" murgan C Programming 6 12-21-2005 06:01 AM
Passing pointer to template function as argument to pointer to template function Vijai Kalyan C++ 4 11-08-2005 07:53 PM
pointer to member function and pointer to constant member function Fraser Ross C++ 4 08-14-2004 06:00 PM
function pointer and member function pointer question glen stark C++ 2 10-10-2003 01:41 PM



Advertisments