Pointer to function prototype in K&R C

H

hansoft

Hi!

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 bla (int (*blabla) ()) {}

or

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

or:

int bla (int (*blabla) (int, int)) {}

or (unlikely):

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

Tnx!!

Hans
 
L

Lew Pitcher

Hi!

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 bla (int (*blabla) ()) {}

No. That's not K&R C
or

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

No. That's not K&R C

or:

int bla (int (*blabla) (int, int)) {}

No. That's not K&R C

or (unlikely):

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

No. That's not K&R C


In K&R C, a function is declared without arguments,
int f();
and defined with arguments that follow the parameter list
int f(a,b)
int a;
char *c;
{ /*body*/ return 0; }

Note that K&R C function declarations do not include parameters, and K&R C
does not provide a means to construct a function prototype;


Now, if
int f(a,b)
int a;
char *c;
{ return *(c+a); }
was the function definition, or
int f();
was the function declaration, then
f
was a pointer to the function.




--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------
 
K

Keith Thompson

I write C based software that I try to keep as portable as possible.
Therefore I support both K&R and ANSI headers.
[...]

Is that really necessary? Are there any systems you care about on
which at least a C89 implementation isn't available?
 
G

Guest

(e-mail address removed) wrote:


     This defines an int-valued function named bla, with one parameter
named blabla.  That parameter is a pointer to an int-valued function
with a fixed (but unknown) number of parameters, whose types are not
described but are all "promoted."



     This defines an int-valued function named bla, with one parameter
named blabla.  That parameter is a pointer to an int-valued function
with two parameters, the first of type bar and the second of type foo
(both foo and bar are, presumably, typedef aliases).



     This defines an int-valued function named bla, with one parameter
named blabla.  That parameter is a pointer to an int-valued function
with two parameters, both of type int.



     This defines an int-valued function named bla, with one parameter
named blabla.  That parameter is a pointer to an int-valued function
with two parameters, both of type int.  The two parameters' names are
given as bar and foo, but that's just decoration; it may serve as
documentation, but means nothing as far as the code is concerned.

jolly good. I note though you didn't answewr the question! And I was,
mildly, interested; and my K&R e1 isn't handy.

is it?
int (*blabla) (int, int);

or
int (*blabla) ();
 
G

Guest

(e-mail address removed) writes:

Is that really necessary?  Are there any systems you care about on
which at least a C89 implementation isn't available?

I occaisionally do maintenance on K&R C. I'm not sure if
a C89 compiler is available for the platform (an old sun)
 
J

James Kuyper

....
jolly good. I note though you didn't answewr the question! And I was,
mildly, interested; and my K&R e1 isn't handy.

is it?
int (*blabla) (int, int);

That's a function pointer declaration using function prototype syntax.
K&R C didn't have function prototypes, which is precisely why this is an
issue.
or
int (*blabla) ();

The latter.
 
E

Eric Sosman

(e-mail address 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:
[...]

jolly good. I note though you didn't answewr the question! And I was,
mildly, interested; and my K&R e1 isn't handy.

is it?
int (*blabla) (int, int);

or
int (*blabla) ();

I didn't answer the question (quoted above) because I
couldn't make out what was being asked. We're told that
hansoft must "support" (whatever that means) "K&R and ANSI
headers" (whatever that means). We're told that one header
contains a pointer to a function with arguments (and it's
not 100% clear what "contains" means), and finally we're
asked what "it" is.

Beats me.

So I settled for a blow-by-blow explication of the four
fragments hansoft posted, in hopes that they might clear up
whatever was troubling him, or that he might post again with
a clearer explanation of "it."

What do *you* mean by "it," and is the same thing
hansoft means?
 
B

Ben Bacarisse

James Kuyper said:
That's a function pointer declaration using function prototype
syntax. K&R C didn't have function prototypes, which is precisely why
this is an issue.


The latter.

That's one answer if you read the OP's words ("in one of these
headers, there is a pointer to a function with arguments") but if you
read the C ("int bla (int (*blabla) ()) {}") then the answer is:

int bla(blabla)
int (*blabla)();
{}

but why does the OP have definition in a header? If we translate
further and assume he means he has a declaration that in new C would
be int bla(int (*blabla) ()); then the answer is even simpler:

int bla();
 
K

Keith Thompson

I occaisionally do maintenance on K&R C. I'm not sure if
a C89 compiler is available for the platform (an old sun)

Well, you could install gcc. Newer versions can't bootstrap from a
pre-ANSI compiler, but older ones can. Whether it's worth the effort
is another question.
 
H

hansoft

Sorry folks,

I did some Googling last night and just didn't get the answer so I
mixed thing up:

The question is:

int blabla (foo, bar)
int foo;
int bar;
{}

int bla (blabla)
int (*blabla) ();
{}

is this correct or do we need:

int (*blabla) (int, int);
int (*blabla) (foo, bar);

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

Hans Bezemer
 
J

jameskuyper

Sorry folks,

I did some Googling last night and just didn't get the answer so I
mixed thing up:

The question is:

int blabla (foo, bar)
int foo;
int bar;
{}

int bla (blabla)
int (*blabla) ();
{}

is this correct or do we need:

Yes, except that bla() doesn't seem to actually do anything, not even
return an int value. It would be clearer if you wrote
{/* details */}
or
{
...

To indicate that there is a function body, but that the details of the
function body aren't needed.
 
C

CBFalconer

.... snip ...

is this correct or do we need:

int (*blabla) (int, int);
int (*blabla) (foo, bar);

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

Bear in mind that K&R declarations do not specify the types of
parameters. It is up to the user to insure that they use correct
types. Therefore the second listing is the K&R one.
 
B

Ben Bacarisse

CBFalconer said:
Bear in mind that K&R declarations do not specify the types of
parameters. It is up to the user to insure that they use correct
types. Therefore the second listing is the K&R one.

Both the examples are syntax errors in K&R C. If the intent (now very
confused) is to declare a pointer to a function returning int, one
would write:

int (*blabla)();

in K&R C.
 
G

Guest

(e-mail address 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:
[...]
jolly good. I note though you didn't answewr the question! And I was,
mildly, interested; and my K&R e1 isn't handy.
is it?
   int (*blabla) (int, int);
or
   int (*blabla) ();

     I didn't answer the question (quoted above) because I
couldn't make out what was being asked.

I just took it to mean he wanted his code to compile on both
K&R compilers and C89++ compilers. I've seen stuff like

#ifdef ANSI
double refrobnicate (int a, double b)
#else
double refrobnicate (a, b)
int a, double b
#endif
{
code_goes_here();
return a - b;
}

and I was assuming he wanted to do similar tricks to support function
pointers. Perhaps I read War And Peace between the lines. I was
thinking he wanted something like

#ifdef ANSI
int (*blabla) (int, int);
#else
int (*blabla) ();
#endif

but I wasn't sure of the form of K&R function pointers.

We're told that
hansoft must "support" (whatever that means) "K&R and ANSI
headers" (whatever that means).  We're told that one header
contains a pointer to a function with arguments (and it's
not 100% clear what "contains" means),

what could he mean besides "declares"?

and finally we're asked what "it" is.

what a function pointer looks like...
     Beats me.

     So I settled for a blow-by-blow explication of the four
fragments hansoft posted, in hopes that they might clear up
whatever was troubling him, or that he might post again with
a clearer explanation of "it."

     What do *you* mean by "it," and is the same thing
hansoft means?

I *think* so, but how could I ever be sure?

<dash><dash><whitespace><newline>SICP is the most important printed
literature to develop from our species in the 20th century. (Amazon
Review)
 
G

Guest

Well, you could install gcc.  Newer versions can't bootstrap from a
pre-ANSI compiler, but older ones can.  Whether it's worth the effort
is another question.

And recompile and *re-test* several thousand lines of working code?
 
K

Keith Thompson

And recompile and *re-test* several thousand lines of working code?

So in your case, the answer is probably "no".

For the OP, though, it might be different. If he's writing *new*
code, there's less likelihood that it actually makes sense to maintain
compatibility with pre-ANSI C.
 
J

Joachim Schmitz

<dash><dash><whitespace><newline>SICP is the most important printed
literature to develop from our species in the 20th century. (Amazon
Review)
a signature that went bad?

Bye, Jojo
 

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,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top