[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

Pointer to function in K&R C

Jack

9/7/2011 8:55:00 PM

Hey there!

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:

int foo (int (*bar) ()) {}

or

int foo (int (*bar) (x, y)) {}

or:

int foo (int (*bar) (int, int)) {}

or (unlikely):

int foo (int (*bar) (int x, int y)) {}

Tnx!!
16 Answers

Harald van D?k

9/7/2011 9:04:00 PM

0

On Sep 7, 10:54 pm, nathan <spamt...@trashcan.invalid> wrote:
> I write C based software that I try to keep as portable as possible.
> Therefore I support both K&R and ANSI headers.

Are there actually any compilers that are of interest to you, that do
not support function prototypes? Prototypes have been standard C for
more than 20 years.

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

In your header:

int foo ();

In your source file:

int foo (bar, x, y)
int (*bar) ();
int x;
int y;
{
return (*bar) (x, y);
}

Keep in mind that K&R C is not a actual standard, and pre-ANSI
compilers vary in behaviour in different aspects.

christian.bau

9/7/2011 9:09:00 PM

0

On Sep 7, 9:54 pm, nathan <spamt...@trashcan.invalid> wrote:
> Hey there!
>
> 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:

I think the last time I heard of anyone giving up on pre-ANSI C
compatibility was about sixteen years ago.
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.

Jorgen Grahn

9/7/2011 10:46:00 PM

0

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 me@student.foo.edu", "w");
fputs(stock_market_of_1992, f);
fclose(f);
}

I'm not rich yet. I guess that means all my programs work.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .

Keith Thompson

9/8/2011 12:41:00 AM

0

Jorgen Grahn <grahn+nntp@snipabacken.se> 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 me@student.foo.edu", "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).

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.ne...
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Jack

9/8/2011 5:42:00 PM

0

On Wed, 07 Sep 2011 14:04:20 -0700, Harald van Dijk wrote:
> On Sep 7, 10:54 pm, nathan <spamt...@trashcan.invalid> wrote:
>> I write C based software that I try to keep as portable as possible.
>> Therefore I support both K&R and ANSI headers.
>
> Are there actually any compilers that are of interest to you, that do
> not support function prototypes? Prototypes have been standard C for
> more than 20 years.
>
>> 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.
>
> In your header:
>
> int foo ();
>
> In your source file:
>
> int foo (bar, x, y)
> int (*bar) ();
> int x;
> int y;
> {
> return (*bar) (x, y);
> }
>
> Keep in mind that K&R C is not a actual standard, and pre-ANSI compilers
> vary in behaviour in different aspects.

Hi Harry,
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) ();
{}

is this correct or do we need:

int (*z) (int, int);
int (*z) (x, y);

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

Best
Nath

Chad

9/8/2011 6:24:00 PM

0

On Sep 7, 2:04 pm, Harald van D?k <true...@gmail.com> wrote:
> On Sep 7, 10:54 pm, nathan <spamt...@trashcan.invalid> wrote:
>
> > I write C based software that I try to keep as portable as possible.
> > Therefore I support both K&R and ANSI headers.
>
> Are there actually any compilers that are of interest to you, that do
> not support function prototypes? Prototypes have been standard C for
> more than 20 years.
>
> > 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.

Chad

Harald van D?k

9/8/2011 7:08:00 PM

0

On Sep 8, 8:23 pm, Chad <cdal...@gmail.com> wrote:
> On Sep 7, 2:04 pm, Harald van D?k <true...@gmail.com> wrote:
> > On Sep 7, 10:54 pm, nathan <spamt...@trashcan.invalid> wrote:
> > > 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.

If K&R C does not have "function with argument" types, then the answer
is "the same way as a pointer to a function without arguments".

Keith Thompson

9/8/2011 7:10:00 PM

0

Chad <cdalten@gmail.com> writes:
> On Sep 7, 2:04 pm, Harald van Dijk <true...@gmail.com> wrote:
>> On Sep 7, 10:54 pm, nathan <spamt...@trashcan.invalid> wrote:
>>
>> > I write C based software that I try to keep as portable as possible.
>> > Therefore I support both K&R and ANSI headers.
>>
>> Are there actually any compilers that are of interest to you, that do
>> not support function prototypes? Prototypes have been standard C for
>> more than 20 years.
>>
>> > 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.

The distinction is that a K&R C function type does not include
information about the function's arguments.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.ne...
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Keith Thompson

9/8/2011 7:18:00 PM

0

nathan <spamtrap@trashcan.invalid> 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), so a pre-ANSI compiler is unlikely
to support it. It's legal in ANSI C or later, but mixing prototypes
and old-style function definitions like that doesn't make much sense.

> 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). (If it is valid, it only tells you how
many parameters the function takes, not their types, and that's not
useful.)

> 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)?

There's an old tool called "ansi2knr" (Google it) that attempts to
convert C code with prototypes into K&R-compatible code without
prototypes. I don't think it was ever 100% functional, but you
might it useful.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.ne...
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Ben Bacarisse

9/8/2011 7:41:00 PM

0

nathan <spamtrap@trashcan.invalid> writes:

> On Wed, 07 Sep 2011 14:04:20 -0700, Harald van Dijk wrote:
>> On Sep 7, 10:54 pm, nathan <spamt...@trashcan.invalid> wrote:
>>> I write C based software that I try to keep as portable as possible.
>>> Therefore I support both K&R and ANSI headers.
<snip>
>> Keep in mind that K&R C is not a actual standard, and pre-ANSI compilers
>> vary in behaviour in different aspects.
>
> Hi Harry,

Harry?

> 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) ();
> {}
>
> is this correct

Yes, for K&R C, that's all you need.

> or do we need:
>
> int (*z) (int, int);
> int (*z) (x, y);

Neither is permitted by the syntax of K&R C.

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

--
Ben.