From: Liwei zou on
hi,everyone
I want to transfer the two dimension array in a subroutine,but it
can't works.
I do that as follow:
program main
real ::array_name(12,4)
...........
call xx(array_name,...) ! in the main program
end

subroutine xx(arrayname,......)
real::arrayname(12,4)
............
endsubroutine

but when I output the data of the two dimension array in the main
program and in subroutine,
I found the data is not the same,but I havn't change the data in the
subroutine.

how to correct it ? thanks a lot
thank for you reply!
liweizou

From: Liwei zou on

I get the point,the size of array which need to be transfer
in main program and subroutine should be the same
thanks

From: FX on
> program main
> real ::array_name(12,4)
> ..........
> call xx(array_name,...) ! in the main program
> end
>
> subroutine xx(arrayname,......)
> real::arrayname(12,4)
> ...........
> endsubroutine
>
> but when I output the data of the two dimension array in the main
> program and in subroutine, I found the data is not the same,but I
> havn't change the data in the subroutine.

You really need to post a small example that reproduces your problem, as
well as details (compiler, compiler options used, execution machine,
etc.) on how you compile & run the code.

From your above blurb, I wrote the following code:

program main
real ::array_name(12,4) = reshape((/ (real(i), i = 1, 48) /), &
(/ 12, 4 /))
print *, array_name
call xx(array_name)
end

subroutine xx(arrayname)
real::arrayname(12,4)
print *, arrayname
endsubroutine

When you run it, it works as you expect, printing out the same values
in the main program and the subroutine.

--
FX
From: Elijah Cardon on

"FX" <coudert(a)alussinan.org> wrote in message
news:egikr7$1qet$1(a)nef.ens.fr...
>> program main
>> real ::array_name(12,4)
>> ..........
>> call xx(array_name,...) ! in the main program
>> end
>>
>> subroutine xx(arrayname,......)
>> real::arrayname(12,4)
>> ...........
>> endsubroutine
>>
>> but when I output the data of the two dimension array in the main
>> program and in subroutine, I found the data is not the same,but I
>> havn't change the data in the subroutine.
>
> You really need to post a small example that reproduces your problem, as
> well as details (compiler, compiler options used, execution machine,
> etc.) on how you compile & run the code.
>
> From your above blurb, I wrote the following code:
>
> program main
> real ::array_name(12,4) = reshape((/ (real(i), i = 1, 48) /), &
> (/ 12, 4 /))
> print *, array_name
> call xx(array_name)
> end
>
> subroutine xx(arrayname)
> real::arrayname(12,4)
> print *, arrayname
> endsubroutine
>
> When you run it, it works as you expect, printing out the same values
> in the main program and the subroutine.

The snippet doesn't compile for me:
Compiling file: array1.f95
C:\Documents and Settings\All Users\Documents\fortran_stuff\array1.F95(3) :
error 921 - The REAL intrinsic function is not permitted in an
initialisation expression
Ideas? EC


From: dpb on

Elijah Cardon wrote:
> "FX" <coudert(a)alussinan.org> wrote in message
....
> > program main
> > real ::array_name(12,4) = reshape((/ (real(i), i = 1, 48) /), &
> > (/ 12, 4 /))
....

> The snippet doesn't compile for me:
> Compiling file: array1.f95
> C:\Documents and Settings\All Users\Documents\fortran_stuff\array1.F95(3) :
> error 921 - The REAL intrinsic function is not permitted in an
> initialisation expression
> Ideas? EC

Interesting, what compiler? The problem is that the compiler didn't
recognize the use of real() in the argument to reshape() as an array,
but as the intrinsic function REAL() which isn't allowable in the
context as the error indicated.

While the use of an intrinsic function as a variable name is allowable
(in at least most places) it isn't good practice leading to difficulty
in reading the code. Here it seems to have found a problem in the
compiler.

The workaround would be to use some other name for the temporary array
in the reshape() argument as in

real ::array_name(12,4) = reshape((/ (r(i), i = 1, 48) /), &
(/ 12, 4 /))

where I used "r" instead of "real".

The use of "implicit none" at the beginning would have caught this out
with a different message of not having defined real() and that might
have triggered the thought of not using the intrinsic name as a
variable originally.