when to ignore warnings

F

Frank

I successfully did fortran/c interop today. It raised warnings from
gcc.exe but behaved:

F:\gfortran\dan>gcc -c -Wall -Wextra d2.c -I F:\gfortran\include
d2.c: In function 'copy':
d2.c:3: warning: type of 'in' defaults to 'int'
d2.c:3: warning: type of 'out' defaults to 'int'
d2.c:4: warning: passing argument 1 of 'strcpy' makes pointer from integer
witho
ut a cast
d2.c:4: warning: passing argument 2 of 'strcpy' makes pointer from integer
witho
ut a cast

F:\gfortran\dan>gfortran c2.f03 d2.o -Wall -Wextra -o out

F:\gfortran\dan>out
hello world from c
1
2
3
4
5
6
7
8
9

=

☺



♫


hello world

F:\gfortran\dan>type d2.c
#include <string.h>
#include <stdio.h>
void copy(in,out) {
strcpy(out,in);
printf("hello world from c\n");
}


// gcc -c -Wall -Wextra d2.c -I F:\gfortran\include
F:\gfortran\dan>type c2.f03
PROGRAM test

use, intrinsic :: iso_c_binding, only: c_char, c_null_char

implicit none

interface
subroutine copy(in, out) bind(c,name='copy')
use, intrinsic :: iso_c_binding, only: c_char
character(kind=c_char), dimension(*) :: in, out
end subroutine copy
end interface

character(len=20, kind=c_char) :: &
digit_string = c_char_'123456789' // c_null_char
character(kind=c_char) :: digit_arr(20)
call copy(digit_string, digit_arr)
print '(1x, a1)', digit_arr(1:19)



print *, "hello world"
END PROGRAM test



! gfortran c2.f03 d2.o -Wall -Wextra -o out
F:\gfortran\dan>

I could add casts to see if the warnings would disappear, but I'd like to
know *why* I'm adding a cast before I do so.

Fishing for tips. Cheers,
--
Frank

I once asked the most fabulous couple I know, Madonna and Guy Ritchie, how
they kept things fresh despite having been married for almost seven months.
'It's a job, Al,' Guy told me. 'We work at it every day.'
~~ Al Franken,
 
K

Keith Thompson

Frank said:
I successfully did fortran/c interop today. It raised warnings from
gcc.exe but behaved:

F:\gfortran\dan>gcc -c -Wall -Wextra d2.c -I F:\gfortran\include
d2.c: In function 'copy':
d2.c:3: warning: type of 'in' defaults to 'int'
d2.c:3: warning: type of 'out' defaults to 'int'
d2.c:4: warning: passing argument 1 of 'strcpy' makes pointer from integer
witho
ut a cast
d2.c:4: warning: passing argument 2 of 'strcpy' makes pointer from integer
witho
ut a cast [...]
F:\gfortran\dan>type d2.c
#include <string.h>
#include <stdio.h>
void copy(in,out) {
strcpy(out,in);
printf("hello world from c\n");
}

Do *not* ignore these warnings.

In your definition of copy(), you haven't specified types for the
parameters. In versions of C prior to C99, the types default to
int, as the compiler warned you (and in C99 your code is illegal).
Since you're passing the parameters as arguments to strcpy(),
that's clearly not what you want.

Change
void copy(in,out)
to
void copy(const char *in, char *out)

[snip]
I could add casts to see if the warnings would disappear, but I'd like to
know *why* I'm adding a cast before I do so.

Hesitating to add casts to silence warnings is a very good instinct.
In this case, it would have been the equivalent of taping over a
warning light on your car's dashboard -- except that you'd still
get the "defaults to 'int'" warnings.

The code probably *seems* to work because int and char* happen
to be the same size and are passed as parameters using the same
mechanism -- on your system.
 

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,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top