- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I have created a data structure that contains an array:
type EXAMPLE
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 13.1.3.198 [Intel(R) 64]...
C:\Win\FEM2000_v25\Modules\FluxLib.f90(53): error #6366: The shapes of the array expressions do not conform.
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?
Thanks
Steve
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Steve,
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/))
Dirk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Stephen, are you sure?
C:\Projects>type t.f90
real a(3,3)
a = (/1,2,3,4,5,6,7,8,9/)
end
C:\Projects>ifort -c t.f90
Intel(R) Visual Fortran Compiler XE for applications running on IA-32, Version 14.0.1.139 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 = (/1,2,3,4,5,6,7,8,9/)
^
compilation aborted for t.f90 (code 1)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
It's curious that the standard requires that for an intrinsic assignment statement:
"the variable and expr shall be conformable" (7.2.1.2, 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 (7.2.1.3) 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 - 7.2.1.3 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.
![](/skins/images/71A9511A625CE5D5D45FB59952A4AA71/responsive_peak/images/icon_anonymous_message.png)
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page