Q re storage alloc stmt

Q re storage alloc stmt

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(1:8) ! does not work
c1=c8(1:8) ! does not work
print *,c1


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

4 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

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'.

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 ! ! !

c1 = transfer(c8,c1)

Leave a Comment

Please sign in to add a comment. Not a member? Join today