Function prototypes for pointers to functions. 
Author Message
 Function prototypes for pointers to functions.

I was playing around with pointers to functions on Borland V4.0. The code
I wrote was something like:

#include <stdio.h>

void bye()
{
  printf("Bye\n");

Quote:
}

int main()
{
  void (*call_bye)() = bye;

  call_bye();
  return 0;

Quote:
}

When I compiled the above code, the borland compiler gave a warning
saying I haven't given a prototype. My question is how do you give a
prototype for a pointer to a function?
Note, however, when I compiled this using the C++ compiler instead of the
C compiler (ie compiled as a .cpp rather than .c file) I didn't get a
warning. Is this a difference between C and C++ or something else?

Thanks.
--
Matthew Li
Bachelor of Electronics/Communications/Computer Systems Engineering
Royal Melbourne Institute of Technology



Tue, 08 Jul 1997 20:54:43 GMT  
 Function prototypes for pointers to functions.

Quoting s934142 from a message in comp.lang.c
   > I was playing around with pointers to functions on Borland V4.0. The
   >code I wrote was something like:
   > #include <stdio.h>
   > void bye()
   > {
   > printf("Bye\n");
   > }
   > int main()
   > {
   > void (*call_bye)() = bye;
   > call_bye();
   > return 0;
   > }
   > When I compiled the above code, the borland compiler gave a warning
   > saying I haven't given a prototype. My question is how do you give a
   > prototype for a pointer to a function?
   > Note, however, when I compiled this using the C++ compiler instead of
   >the C compiler (ie compiled as a .cpp rather than .c file) I didn't
   >get a warning. Is this a difference between C and C++ or something else?

How about:

        void (*call_Bye)(void) = bye;

Bob Withers

Only thing worse than another birthday, not having another birthday.

Rainbow V 1.11 for Delphi - Test Drive



Wed, 09 Jul 1997 06:20:07 GMT  
 Function prototypes for pointers to functions.

Quote:
> I was playing around with pointers to functions on Borland V4.0. The code
> I wrote was something like:

> #include <stdio.h>

> void bye()

Make this line:   void bye(void)

Quote:
> {
>   printf("Bye\n");
> }

> int main()
> {
>   void (*call_bye)() = bye;

>   call_bye();
>   return 0;
> }

See if that helps

+---------------------------------------------------------------+


| Royal Oak              |  voice:  +64 9 4202554               |
| Auckland               | mobile:  +64 2 5981133 (+vmail)      |
| New Zealand            |    fax:  +64 9 4202762               |
+---------------------------------------------------------------+



Wed, 09 Jul 1997 10:36:34 GMT  
 Function prototypes for pointers to functions.

Quote:
> I was playing around with pointers to functions on Borland V4.0. The code
> I wrote was something like:
> #include <stdio.h>
> void bye()
> {
>   printf("Bye\n");
> }
> int main()
> {
>   void (*call_bye)() = bye;
>   call_bye();
>   return 0;
> }
> When I compiled the above code, the borland compiler gave a warning
> saying I haven't given a prototype. My question is how do you give a
> prototype for a pointer to a function?
> Note, however, when I compiled this using the C++ compiler instead of the
> C compiler (ie compiled as a .cpp rather than .c file) I didn't get a
> warning. Is this a difference between C and C++ or something else?

In C a prototype must include a non-empty argument list.  For example,

        void f();       /* is not a prototype */
        void g(void);   /* is a prototype */

f() is a function taking unspecified arguments; g is a function taking no
arguments.

This applies to pointer to function also.  Just change your declaration to

        void (*call_bye)(void) = bye;

Note that the code you have is correct -- using a prototype is not required
by the standard but it useful since it allows for better type checking.

There is a difference between C and C++ here.  In C++ declaring a function
with an empty argument list means that the function takes no arguments.
All function declarations in C++ are prototypes in the C sense.

--
Mike Rubenstein



Wed, 09 Jul 1997 19:32:49 GMT  
 Function prototypes for pointers to functions.

Quote:
>I was playing around with pointers to functions on Borland V4.0. The code
>I wrote was something like:
>#include <stdio.h>
>void bye()
>{
>  printf("Bye\n");
>}
>int main()
>{
>  void (*call_bye)() = bye;
>  call_bye();
>  return 0;
>}
>When I compiled the above code, the borland compiler gave a warning
>saying I haven't given a prototype. My question is how do you give a
>prototype for a pointer to a function?

Both of the functions in this code are defined in traditional
K&R style and there are indeed no prototypes.

If you replace the first three of the four instances of `()'
with `(void)', you will have prototypes.  If your compiler now
stops complaining, then that's all it was.  (This was a stab in
the dark, of course, since you didn't actually tell us what the
compiler really said.  So it might be wrong.)

Quote:
>Note, however, when I compiled this using the C++ compiler instead of the
>C compiler (ie compiled as a .cpp rather than .c file) I didn't get a
>warning. Is this a difference between C and C++ or something else?

One of the many differences between C and C++ is that a function
definition with no parameters in C++ means a function that takes
no arguments, whereas in C it means a function with an unknown
number of unknown types of arguments -- the keyword `void' is
needed here in C to make these functions the sort that don't
have any arguments.

Yet another reason not to use a C++ compiler to compile C code.

--



Fri, 11 Jul 1997 13:13:29 GMT  
 Function prototypes for pointers to functions.
<snip>
|> One of the many differences between C and C++ is that a function
|> definition with no parameters in C++ means a function that takes
|> no arguments, whereas in C it means a function with an unknown
                                                       ^^^^^^^^^^

Is better stated as `a _fixed_ but unknown'.

|> number of unknown types of arguments -- the keyword `void' is
|> needed here in C to make these functions the sort that don't
|> have any arguments.

Because of a very common misconception that printf does not need to be declared
(because a declaration `int func()' is automatically assumed), I am being
pedantic here.

Cheers
Tanmoy
--

Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87544-0285,USA H:#3,802,9 St,NM87545
Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
<http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]



Sat, 12 Jul 1997 00:32:53 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. HELP: function prototype with pointer to function as parameter

2. Function returning a function-pointer - how to prototype ?

3. How to use (pointer to function), and function and pointer to (pointer to function)

4. Pointer Functions and Pointers to Pointer Functions

5. C Prototype for FARPROC function pointer

6. function pointers and prototypes

7. Prototype for function returning funtion pointer

8. Prototype for Functions pointers in Structures

9. Help. pointer to function prototype

10. How to prototype a function pointer?

11. HOW: Pointer to char array ANSI function prototype

12. Prototypes and arrays of pointer to function.

 

 
Powered by phpBB® Forum Software