LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Fortran type conversion error in block data (https://www.linuxquestions.org/questions/programming-9/fortran-type-conversion-error-in-block-data-651672/)

jhwilliams 06-25-2008 05:12 PM

Fortran type conversion error in block data
 
I'm trying to compile a Fortran program, but am having trouble converting from int to char. I looked into the ICHAR procedure offered with the GNU compiler, but I haven't figured out how to use it since my code is within a block data section. Does anyone have an idea how to change these lines so that I don't get the error? (or is there a compiler option?)

Code:

gfortran -c -I./included_dir -Werror -Wtabs -fall-intrinsics myfortranfile.f
myfortranfile.f:107.15:

      DATA NUM/'0  ','1  ','2  ','3  ','4  ','5  ','6  ',
              1
Error: Incompatible types in assignment at (1), CHARACTER(1) to INTEGER(4)
myfortranfile.f:106.17:

      DATA LEVEL/'+  ','/  ','    ','*  ','0  ','B  '/
                1
Error: Incompatible types in assignment at (1), CHARACTER(1) to INTEGER(4)
make: *** [myfortranfile.o] Error 1

Thanks,
Jameson

colucix 06-26-2008 10:09 AM

The ichar function returns the ASCII code of the characters. To convert from int to char and vice versa you have to use internal file I/O, that is:
Code:

READ(STRING, '(I5)') IVAR    ! converts a numeric string to integer
WRITE(STRING, '(I5)') IVAR  ! converts an integer to a string

but you can do that when assigning values in a DATA statement. You have to respect the type of the variable and eventually do the conversion inside the code when you need them. Also from the second line of your error message:
Code:

DATA LEVEL/'+  ','/  ','    ','*  ','0  ','B  '/
I don't really understand how do you want to convert symbols to integer, unless you really want their ASCII code, in which case I have misunderstood your post.


All times are GMT -5. The time now is 05:24 AM.