J3/98-101 Date: 20 November 1997 To: J3 From: Richard Maine Subject: Interpretation Request on Character Array Constructors NUMBER: TITLE: Character array constructors KEYWORDS: Character, array constructor, zero-size DEFECT TYPE: STATUS: The f95 standard appears to leave the length of zero-sized character array constructors undefined. The penultimate paragraph of clause 4.5 says "The type and parameters of an array constructor are those of the expressions." If there are no elements, then the values of the type parameters might not be defined. The type and kind are always well defined at compile time. However, the length parameter for zero-sized character array constructors is not obviously well-defined, even at run-time. Zero-sized constructors are certainly allowed, as mentioned in the antipenultimate paragraph of clause 4.5: "An empty sequence forms a zero-sized rank-one array." Experimentation with different f90 compilers yields varying results. In the sample of question 2 below, one compiler gave a compile-time error message "array constructor has indeterminate character length". (Interestingly, it also gave that message when n=1, which should have been valid; some other variations gave compiler crashes). Another compiler gave the lengths as 2 and 5 and the n_calls as 1, obviously evaluating the presumed first element, even when the result had, in fact, no elements. QUESTION 1: Is the following a valid program and, if so, what len value does it print. program zero write (*,*) len( (/ ('abc', i = 1 , 0) /) ) end QUESTION 2: Is the following a valid program and, if so, what values does it print for the lengths and for n_calls. program zero_size integer :: n = 0 integer :: n_calls = 0 call sub('abc', n) write (*,*) 'n_calls = ', n_calls contains subroutine sub(string, n) character*(*) :: string integer :: n integer :: i write (*,*) len( (/ (string(:twice(i)), i = 1 , n) /) ) write (*,*) len( (/ (string(:twice(i)), i = 1 , n), 'abcde' /) ) end subroutine sub function twice(i) integer :: twice twice = 2*i n_calls = n_calls + 1 end function twice end program zero_size ANSWER: EDIT: SUBMITTED BY: Richard Maine HISTORY: submitted 20 Nov 1997