Calling a function by ptr, getting a compiler warning 
Author Message
 Calling a function by ptr, getting a compiler warning


Quote:

>// Does anyone know how to avoid a compiler warning message "function
>// called with no prototype" when calling a function via a pointer? I
>// can disable the warning with a pragma, but it seems like C should
>// allow me to make such a call without a warning.  The compiler is
>// not performing parameter checking on this call either (i.e. I can
>// pass parameters into a function that takes none).

>// I'm using BC45 (straight C compiles).

>// Here's the example:

>#include <stdio.h>

>int FunctionA ( void ) ;
>void FunctionB ( void ) ;

>int FunctionA ( void ) {
>  int retVal = 1 ;
>  printf ( "In FunctionA\n" ) ;
>  return retVal ;
>}

>void FunctionB ( void ) {
>  int status = 0 ;

>  int (*fptr) () = FunctionA ;

Aren't compliers fun.  BC 3.1 issued the warning you mentioned, but
MSVC warned about the mismatch in this line instead.

Why are you declaring fptr as a pointer to a function with no info
on its params and returning an int, assignning it the address of
a function that takes no params and returns int, then wondering
when the compiler questions it?  

In ansi c defining a function using () simply turns off arg list
checking for the function (this is for compatibility with older
C programs).  You must use (void) to specify an empty list.

Try:

int (*fptr) (void) = FunctionA ;

(BTW in c++ () and (void) work the same.  So your code compiles
without error as c++. Not that this is relevant in c.l.c. <g>)

Quote:

>  printf ( "In FunctionB\n" ) ;

>  status = (*fptr) () ; // this line generates the warning

Both compilers issued an error here.  Something about the // c++
comment bothered my c compilers.  Imagine that.

--
John R Buchan           -:|:-     Looking for that elusive FAQ?  ftp to:



Tue, 24 Mar 1998 03:00:00 GMT  
 Calling a function by ptr, getting a compiler warning

Quote:
Austin ) writes:

|>
|>
|> // Does anyone know how to avoid a compiler warning message "function
|> // called with no prototype" when calling a function via a pointer? I

Yes.

|> // can disable the warning with a pragma, but it seems like C should
|> // allow me to make such a call without a warning.  The compiler is

C does. But you must first learn C. (// is a syntax error in C, not a comment
start character. The _only_ way to comment something in C is start with /*
and end with */

|> // not performing parameter checking on this call either (i.e. I can
|> // pass parameters into a function that takes none).
|>
|> // I'm using BC45 (straight C compiles).

Obviously not if it accepts //. The C language standard says that a compiler
must detect and diagnose syntax errors like //.

|> void FunctionB ( void ) ;
|>
|> int FunctionA ( void ) {
<snip>
|> }
|>
|> void FunctionB ( void ) {
<snip>
|>   int (*fptr) () = FunctionA ;

Make int `int (*fptr) (void) = FunctionA ;'

<snip>
|>   status = (*fptr) () ; // this line generates the warning
|> }
|>
|> main ( )

Better make this `int main (void)'. `main()' in this context means exactly
the same, but it is good habit to write the full form.

|> {
|>  FunctionB () ;
|>  return 1 ;
|> }

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 ]



Tue, 24 Mar 1998 03:00:00 GMT  
 Calling a function by ptr, getting a compiler warning
|> // Does anyone know how to avoid a compiler warning message "function
|> // called with no prototype" when calling a function via a pointer? I
|> // can disable the warning with a pragma, but it seems like C should
|> // allow me to make such a call without a warning.  The compiler is
|> // not performing parameter checking on this call either (i.e. I can
|> // pass parameters into a function that takes none).
|>
|> // I'm using BC45 (straight C compiles).
|>
|> // Here's the example:
|>
|> #include <stdio.h>
|>
|>
|> int FunctionA ( void ) ;
|> void FunctionB ( void ) ;
|>
|> int FunctionA ( void ) {
|>   int retVal = 1 ;
|>   printf ( "In FunctionA\n" ) ;
|>   return retVal ;
|> }
|>
|> void FunctionB ( void ) {
|>   int status = 0 ;
|>
|>   int (*fptr) () = FunctionA ;
|>
|>   printf ( "In FunctionB\n" ) ;
|>
|>   status = (*fptr) () ; // this line generates the warning

I'm not surprised...The // is illegal in ANSI C. I'm surprised you
did not get warnings further up for those other comments if you
really did do "straight C compiles". Anyway you don't even tell us
what the warning is, but this code, minus the comments, works fine
for me. Personally I would have done:

int (*fptr)(void) = FunctionA;

Although that probably won't make much difference.

|>   printf ( "status = %d\n" , status ) ;
|>
|> }
|>
|> main ( )
|> {
|>  FunctionB () ;
|>  return 1 ;

Why 1? Or because you *know* your program is going to fail?

|> }
--
"If it wasn't for C, we would be using BASI, PASAL, and OBOL."




Tue, 24 Mar 1998 03:00:00 GMT  
 Calling a function by ptr, getting a compiler warning
|> What do you mean? On _my_ system 1 means success... (Hint: only 0,
|> EXIT_SUCCESS and EXIT_FAILURE are "portable"; all others are implementation
|> defined. "portable", because the concept of (un)successful termination is
|> already implementation defined).

I know that EXIT_FAILURE is implementation defined, but it was my
understanding that a 'return(0);' inside of main() returns to the operating
systems whatever is a successful return status for that OS if applicable.
This could be in error as the FAQ only states that main() returns an int,
but does not specify what any particular int would necessarily mean.
--
"If it wasn't for C, we would be using BASI, PASAL, and OBOL."




Thu, 26 Mar 1998 03:00:00 GMT  
 Calling a function by ptr, getting a compiler warning

writes:
|> |> What do you mean? On _my_ system 1 means success... (Hint: only 0,
|> |> EXIT_SUCCESS and EXIT_FAILURE are "portable"; all others are
implementation
|> |> defined. "portable", because the concept of (un)successful termination
is
|> |> already implementation defined).
|>
|> I know that EXIT_FAILURE is implementation defined, but it was my
|> understanding that a 'return(0);' inside of main() returns to the operating
|> systems whatever is a successful return status for that OS if applicable.

Yes, 0 means success, but 1 does _not_ necessarily mean failure. It could
mean `success' or `failure' or something more specific depending on the
implementation.

So, 1 _could_ mean success as claimed by the poster in a conforming
implementation.

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 ]



Thu, 26 Mar 1998 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Getting function Ptr dynamically

2. Getting rid of VC++ Compiler warnings

3. Getting better compiler warnings for bad habits

4. Getting rid of VC++ Compiler warnings

5. ptr to ptr to ptr..... n times

6. Help, DOS Borland Compiler Warning -- ANSI Peek Function

7. free as a function argument and compiler warning on prototype

8. Warning Call to Function w/no Prototype in

9. C4201 and C4214 warnings at Compiler Warning Level 4

10. function returning a function ptr

11. typedef the function or the function ptr?

12. Function that returns a function ptr

 

 
Powered by phpBB® Forum Software