Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.

Q re storage alloc stmt

WSinc
New Contributor I
482 Views

I want the leters a-h to go into separate cells in the C1 array.

character*8 c8

character*1 c1(8)

data c8/"ABCDEFGH"/

c1(1:8)=c8

c1(1:8)=c8(1:8) ! does not work
c1=c8(1:8) ! does not work
print *,c1

end

Other than a DO LOOP, how can I do this?
I think the compiler gets confused - - -

0 Kudos
3 Replies
IanH
Honored Contributor III
482 Views
You could use a forall statement. This is a flexible form of assignment.

forall (i=1:size(c1)) c1(i) = c8(i:i)

Or, you could use an array constructor:

c1 = [(c8(i:i), i=1, size(c1))]

Whether the above are better or worse than a DO construct is probably down to personal preference. In both i is of type integer.

Background for what you've observed, just for clarity:

- c8 is a scalar with length 8. c1 is an array of size 8 with length 1. All elements in an array have the same length.

- When you assign an scalar to an array all elements of the array are set to the scalar. You have the equivalent of c1(1) = c8 ; c1(2) = c8 ; ... ; c1(8) = c8.

- Character assignment truncates or pads to fit the destination variable - consequently your three examples would have set each element of c1 to be 'A'.
0 Kudos
WSinc
New Contributor I
482 Views
What you say makes sense -

But I thought that using the range info (on C8) would modify the rule about that.

Apparently not, however.

Thanks ! ! !
0 Kudos
JVanB
Valued Contributor II
482 Views
c1 = transfer(c8,c1)
0 Kudos
Reply