Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fortran 2D array initialization

The following program compiles with ifort (version 12) but not with GFortran (up to version 4.8):

PROGRAM TEST
IMPLICIT NONE
REAL,DIMENSION(2,2)::X=(/1,2,3,4/)

WRITE(*,*) X

END PROGRAM TEST

GFortran gives the error

REAL,DIMENSION(2,2)::X=(/1,2,3,4/)  
                       1  
Error: Incompatible ranks 2 and 1 in assignment at (1)

Ifort compiles the program and gives the expected output. Is this a bug in GFortran or does intel fortran simply allow non-standard array initialization?

like image 513
user1390070 Avatar asked Jan 16 '23 17:01

user1390070


1 Answers

Re-write array declaration line as:

REAL,DIMENSION(2,2) :: X = RESHAPE([1,2,3,4],[2,2])

The reason ifort compiled it the other way is non-standard implementation. This is a way you can initialize arrays of rank higher than 1.

like image 99
milancurcic Avatar answered Mar 29 '23 02:03

milancurcic