Initialisation of array in data structure

Initialisation of array in data structure

I have created a data structure that contains an array:

  real*8 :: T(3,3)
end type
real*8 :: T8(3,3) 

type(EXAMPLE) :: array

To initialise a normal variable array like T8 I can use the following statement:

T8 = (/1.0d0,0.0d0,0.0d0,0.0d0,1.0d0,0.0d0,0.0d0,0.0d0,1.0d0/)

however when I try to do this with the data structure thus:

array%T =  (/1.0d0,0.0d0,0.0d0,0.0d0,1.0d0,0.0d0,0.0d0,0.0d0,1.0d0/)

I get the following compiler error:

Compiling with Intel(R) Visual Fortran Compiler XE [Intel(R) 64]...

C:\Win\FEM2000_v25\Modules\FluxLib.f90(53): error #6366: The shapes of the array expressions do not conform. [T]
compilation aborted for C:\Win\Modules\FluxLib.f90 (code 1)

However when I use this:

array%T(1,1:3) = (/1.0d0,0.0d0,0.0d0/)
array%T(2,1:3) = (/0.0d0,1.0d0,0.0d0/)
array%T(3,1:3) = (/0.0d0,0.0d0,1.0d0/)

there is no problem.

Is this behavior expected?



6 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.


I would expect that you need the following command for both data structure initializations:

T8 = RESHAPE((/1.0d0,0.0d0,0.0d0,0.0d0,1.0d0,0.0d0,0.0d0,0.0d0,1.0d0/),(/3,3/))


Hi Dirk,

I agree that both data initialisations should be consistent but they aren't. The reshape function does not seem to be required for arrays of statndard data types. I'd be interested to known if this is intentional or a compiler glitch.

Stephen, are you sure?

C:\Projects>type t.f90
real a(3,3)
a = (/1,2,3,4,5,6,7,8,9/)
C:\Projects>ifort -c t.f90
Intel(R) Visual Fortran Compiler XE for applications running on IA-32, Version Build 20131008
Copyright (C) 1985-2013 Intel Corporation.  All rights reserved.

t.f90(2): error #6366: The shapes of the array expressions do not conform.   [A]

a = (/1,2,3,4,5,6,7,8,9/)
compilation aborted for t.f90 (code 1)

Retired 12/31/2016

Ah my mistake I see now,

The situation where you can do it is this when variable is declared.

real:: a(3,3) = (/1,2,3,4,5,6,7,8,9/)

Why is this not allowed in executable part as omitting the reshape function could default to shape of the original declaration?

I suppose the shape of the arry could be changed anywhere during the program execution so I think I understand why you need to specify the shape when assigning a list of values in a non-declaration situation.

It's curious that the standard requires that for an intrinsic assignment statement:

"the variable and expr shall be conformable" (, paragraph 1, (3))

The definition of "conformable" is "(of two data entities) having the same shape, or one being an array and the other being scalar"

So in the context of an intrinsic assignment statement, the shapes are required to be the same (there's an exception for an allocatable array for the variable, though the rank can't change.)

But for initialization, traditionally DATA let you initialize multidimensional arrays with a series of values and this carried over to initialization on a type declaration statement. There, the standard says:

"if necessary, the value is converted according to the rules of intrinsic assignment ( to a value that agrees in type, type parameters, and shape with the variable."

Note that the section referenced isn't the same as for the intrinsic assignment statement - says:

"If the variable is an array, the assignment is performed element-by-element on corresponding array elements of the variable and expr."

So the rules really are different and the compiler correctly enforces that.

Retired 12/31/2016

Leave a Comment

Please sign in to add a comment. Not a member? Join today