g95 Can't convert CHARACTER to INTEGER

902 views
Skip to first unread message

louisJ

unread,
Apr 19, 2007, 5:17:59 AM4/19/07
to gg95
Hi

I am a beginner at programming with fortran and g95.

I need to compile and run a fortran77 source file with g95, but I
cannot modify this source file (which was made by a f90 user, so it
compiles good with it).

But when I type:
g95 myfile.f
I get the following error message:
parameter (idtype = 4, inopo = 29, intacm = 32, nopo =
'NOPO')

1
Error: Can't convert CHARACTER(1) to INTEGER(4) at (1)

In the code the line just before this flagged line is:
integer idtype, inopo, intacm, nopo

Also it seems that there is a type conversion problem.

Is there an option in g95 that allows it to make implicit type
conversion between character and integer.

Don

unread,
Apr 19, 2007, 9:06:54 AM4/19/07
to gg95
Early versions of FORTRAN allowed characters to be stored in integer
variables, however more recent versions do not. Normally, FORTRAN
assumes all variables beginning with the letters I thru N to be
integers, so unless you have explicitly defined NOPO as a character
variable you can not use it to store characters as you indicated.

Don

louisJ

unread,
Apr 19, 2007, 1:46:21 PM4/19/07
to gg95
Hi Don,

thank you for answering my message.

Actually, NOPO is declared as an integer!
Here:


integer idtype, inopo, intacm, nopo

parameter (idtype = 4, inopo = 29, intacm = 32, nopo = 'NOP0')

so nopo is declared as an integer and then attributed a string, this
is what I don't understand.
When I put a "print*, nopo" after those lines I get the value
810569550.
Isn't this weird?

> > conversion between character and integer.- Masquer le texte des messages précédents -
>
> - Afficher le texte des messages précédents -

Doug

unread,
Apr 19, 2007, 2:21:48 PM4/19/07
to gg95

On Apr 19, 1:46 pm, louisJ <plumera...@gmail.com> wrote:
> Hi Don,
>
> thank you for answering my message.
>
> Actually, NOPO is declared as an integer!
> Here:
> integer idtype, inopo, intacm, nopo
> parameter (idtype = 4, inopo = 29, intacm = 32, nopo = 'NOP0')
>
> so nopo is declared as an integer and then attributed a string, this
> is what I don't understand.
> When I put a "print*, nopo" after those lines I get the value
> 810569550.
> Isn't this weird?

Try changing the two lines you posted to something like:

INTEGER :: idtype, inopo, intacm
CHARACTER(LEN=4) :: nopo
PARAMETER (idtype = 4, inopo = 29, intacm = 32)
nopo='NOP0'

Doug

Robert Funnell

unread,
Apr 19, 2007, 2:45:17 PM4/19/07
to gg95
Some compilers, but apparently not g95, will permit you to assign a
string to an integer. If the integer is 4 bytes, then a 4-character
string fits just fine. If you then try printing it with a * format,
however, you're asking that it be treated as an integer, so you get
some 'weird' combination of the numeric character codes. This is
normal.

If you really can't change the source code to avoid this non-standard
(but in the past quite popular) trick, then I don't know what you can
do.

Steven Winfield

unread,
Apr 19, 2007, 3:05:25 PM4/19/07
to gg...@googlegroups.com
I've only vaguely been following this thread, but I wonder if the
transfer() function be of any use to you in this situation.

Steve.

louisJ

unread,
Apr 20, 2007, 5:49:07 AM4/20/07
to gg95
Hi Steven, Don, Robert, Doug,

Thank you very much for your answers.

Actually, as Robert says, this was apparently a popular trick in the
past...but it is really non standard! Also I obtained the permission
to modify the code.

I tried to use the advice of Steven...the fortran TRANSFER
function.....and it works!
This function is great.
I put
nopo = TRANSFER('NOP0',nopo)
and it is allright.

Thank you all!
LouisJ

Reply all
Reply to author
Forward
0 new messages