Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations KootK on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

derived type constructors

Status
Not open for further replies.

rih5342

Marine/Ocean
May 8, 2007
40

Hello, I'm using gcc 6.3 on w7-64, with these gfortran compile flags
FLAGS = -std=f2008 -O2 -Wall -Wextra -fno-range-check

Is this legal code?

Code:
    MODULE WAV

    integer, parameter :: int2  = selected_int_kind(2)
    integer, parameter :: int4  = selected_int_kind(4)
    integer, parameter :: int8  = selected_int_kind(8)
    integer, parameter :: int16 = selected_int_kind(16)
    integer, parameter :: SP    = selected_real_kind(4)
    integer, parameter :: DP    = selected_real_kind(8)
    integer, parameter :: QP    = selected_real_kind(16)

    TYPE                :: HEADER       ! DERIVED TYPE
      CHARACTER (LEN=8) :: FILE_ID
      INTEGER(INT8)     :: FILE_LENGTH
      CHARACTER(LEN=8)  :: FILE_TYPE
      CHARACTER(LEN=8)  :: SUBHEADER1_FORMAT
      INTEGER(INT8)     :: SUBHEADER1_LENGTH
      INTEGER(INT8)     :: AUDIO_FORMAT
      INTEGER(INT8)     :: NUM_CHAN
      INTEGER(INT8)     :: SAMPLE_RATE
      INTEGER(INT8)     :: BYTE_RATE
      INTEGER(INT8)     :: BLOCK_ALIGN
      INTEGER(INT8)     :: BITS_PER_SAMP
      CHARACTER(LEN=8)  :: SUBHEADER2_FORMAT
      INTEGER(INT8)     :: SUBHEADER2_LENGTH  
    END TYPE

    CONTAINS

    SUBROUTINE CLEAR_HEADER(W_HEADER)

    IMPLICIT NONE
    TYPE (HEADER), INTENT(OUT) :: W_HEADER
    CHARACTER (LEN=8), PARAMETER :: CHAR8 = '        '

    W_HEADER % FILE_ID            = CHAR8   !  01
    W_HEADER % FILE_LENGTH        = 0_INT8  !  02
    W_HEADER % FILE_TYPE          = CHAR8   !  03
    W_HEADER % SUBHEADER1_FORMAT  = CHAR8   !  04
    W_HEADER % SUBHEADER1_LENGTH  = 0_INT8  !  05
    W_HEADER % AUDIO_FORMAT       = 0_INT8  !  06
    W_HEADER % NUM_CHAN           = 0_INT8  !  07
    W_HEADER % SAMPLE_RATE        = 0_INT8  !  08
    W_HEADER % BYTE_RATE          = 0_INT8  !  09
    W_HEADER % BLOCK_ALIGN        = 0_INT8  !  10
    W_HEADER % BITS_PER_SAMP      = 0_INT8  !  11
    W_HEADER % SUBHEADER2_FORMAT  = CHAR8   !  12
    W_HEADER % SUBHEADER2_LENGTH  = 0_INT8  !  13

    W_HEADER = [CHAR8, 0_INT8, CHAR8, CHAR8, 0_INT8, 0_INT8, 0_INT8, &
               0_INT8, 0_INT8, 0_INT8, 0_INT8, CHAR8, 0_INT8 ]

    END SUBROUTINE CLEAR_HEADER

    END MODULE WAV

I get this compile error.

Code:
    W_HEADER = [CHAR8, 0_INT8, CHAR8, CHAR8, 0_INT8, 0_INT8, 0_INT8, &
                     1
    Error: Element in CHARACTER(1) array constructor at (1) is INTEGER(4)
    make: *** [makefile:12: ref_time_source.o] Error 1

Thank you.
 
Replies continue below

Recommended for you

That is because [], like (/ /) is used for arrays. To assign to a type you could use one of the following

Code:
W_HEADER = HEADER(FILE_ID = CHAR8, FILE_LENGTH = 0_INT8, ...)
W_HEADER = HEADER(CHAR8, 0_INT8, ...)

Note that in the first example, since it is parameter driven, the members do not have to be in order
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor