Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

array initialization

Roman1
New Contributor I
1,403 Views

Hi,

When I compile the following program using the Intel Fortran compiler,
there are no error or warning messages printed. However, I think the code
has a bug because a 2D array is initialized with a 1D array.


[fortran]program test_array
implicit none

integer:: a(2,2) = (/ 1, 2, 3, 4  /)   !!!

print*, sum(a)
stop
end program test_array
[/fortran]


Roman
0 Kudos
3 Replies
Steven_L_Intel1
Employee
1,403 Views
This code is legal and standard Fortran. The standard (F2008) 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."

Then 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."

This is a bit looser than if you had an actual assignment statement but matches what a DATA statement can do.
0 Kudos
Roman1
New Contributor I
1,403 Views

Thanks for the reply.

The reason for the question is that when I compiled the same code using gfortran, I got the
following error:
Error: Incompatible ranks 2 and 1 in assignment at (1)
So perhaps, there is a problem in gfortran.

Also, the Intel compiler does issue a warning if parameter is used.
[bash]integer,parameter:: a(2,2) = (/ 1, 2, 3, 4  /)   !!!
[/bash]
warning #6366: The shapes of the array expressions do not conform.

0 Kudos
Steven_L_Intel1
Employee
1,403 Views
The warning for the parameter is correct.
0 Kudos
Reply