×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

derived type constructors

derived type constructors

derived type constructors

(OP)

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 --> fortran

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.

RE: derived type constructors

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

RE: derived type constructors

(OP)
Got it, thank you.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members! Already a Member? Login


Resources


Close Box

Join Eng-Tips® Today!

Join your peers on the Internet's largest technical engineering professional community.
It's easy to join and it's free.

Here's Why Members Love Eng-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close