我希望有一个过程,它将输入字符a
的本地副本program test_copystr
character(len=6) :: str
str = 'abc'
call copystr(str)
contains
subroutine copystr(a)
character(len=*), intent(in) :: a
!> Local variables
integer :: i
character, allocatable :: b(:)
allocate(b(len_trim(a)))
do i=1, len_trim(a)
b(i) = a(i:i)
end do
print *, b
b(1:len_trim(a)) = a(1:len_trim(a))
print *, b
end subroutine copystr
end program test_copystr
(假定长度不是)放入可分配的字符数组中。我有以下代码
a
我试图以两种不同的方式将b
分配给 #slider {
position: relative;
overflow: hidden;
margin: 20px auto 0 auto; /* take out margin-top. This will give 20px margin to the top and center your slider */
width: 90%; /* less than 100% width gives margins to the sides */
height: 500px;
}
#slider ul li {
position: inherit;
display: block;
float: left;
margin: 0 auto; /* this will center your images inside #slider */
padding: 0;
width: 100%; /* this will fill the container of your slider */
height: 500px; /* depends on the actual size of your images */
}
。结果是
ABC
AAA
我认为两个作业都应该产生相同的输出。任何人都可以解释我的区别吗? (要编译此代码我使用gfortran 5.2.0编译器。)
答案 0 :(得分:3)
如您所知b
是一个字符数组,而a
是标量;当子程序被调用时,它是一个6个字符的字符串。这些是不同的东西。声明
b(1:len_trim(a)) = a(1:len_trim(a))
指定lhs上的数组部分b(1:3)
,即b
的所有3个元素,以及rhs上的子字符串a(1:3)
。现在,当将长度为3的子字符串分配给单个字符(例如b
的任何元素时,Fortran仅分配字符串的第一个字符。
在这种情况下,b
的每个元素都设置为a
的第一个字符。就像编译器生成3个语句
b(1) = 'abc'
b(2) = 'abc'
b(3) = 'abc'
实现数组赋值。这就是Fortran的数组语法对lhs上的数组和rhs上的标量(表达式)的作用,它将标量广播到数组的每个元素。
您使用的第一种方法,循环遍历b
的元素和a
的字符是常规方法,使字符数组等同于字符串。但您可以尝试transfer
- 请参阅我对此问题的回答Removing whitespace in string