Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Initialization of character array

857 views
Skip to first unread message

Lorents

unread,
Apr 6, 2015, 2:46:29 PM4/6/15
to
I would like to initialize an array of type character using an array
constructor as exemplified by the following code:

program mytest
integer, parameter :: wl=30
integer, parameter :: Nobjects =3
character(len=wl), parameter :: CLASSNAMES(1:Nobjects) =
(/"Cat","Rhino","Dolphin"/)

write(*,*) CLASSNAMES

end program mytest


I'm using the Intel Fortran compiler v.12.1.2 under Linux. The code
compiles and works okay when compiled with 'ifort' without any option
but when I specify '-stand f95' (check conformance to Fortran 95) I get
the following warning:

warning #8208: If type specification is omitted, each ac-value
expression in the array constructor of type CHARACTER must have the
same length type parameters.

In fact adding spaces at the end of the string so that they all have the
same length (e.g. "Cat ") gets rid of the warning; is there anything
I can do to avoid the warning without having to do this padding by hand
for all strings?
If I drop the 'parameter' attribute I can initialize the character array
with a DATA statement, in which case strings of different lengths work
okay (ie, no warning is issued); nevertheless, I'd prefer to have the
'parameter' attribute as those strings should never be changed in the
program.
Besides, why the error message says 'If type specification is omitted'?
Am I not specifying that the array is of type character?


Thanks, L.

Steve Lionel

unread,
Apr 6, 2015, 3:12:29 PM4/6/15
to
On 4/6/2015 2:46 PM, Lorents wrote:

> character(len=wl), parameter :: CLASSNAMES(1:Nobjects) =
> (/"Cat","Rhino","Dolphin"/)

>
> I'm using the Intel Fortran compiler v.12.1.2 under Linux. The code
> compiles and works okay when compiled with 'ifort' without any option
> but when I specify '-stand f95' (check conformance to Fortran 95) I get
> the following warning:
>
> warning #8208: If type specification is omitted, each ac-value
> expression in the array constructor of type CHARACTER must have the
> same length type parameters.
>
> Besides, why the error message says 'If type specification is omitted'?
> Am I not specifying that the array is of type character?

You are specifying that CLASSNAMES is type CHARACTER(len=wl) but not the
array constructor. The standard requires that all values in an array
constructor have the same type and type parameters. Length is a type
parameter.

Intel Fortran has an extension where it will pad the shorter values to
the length of the longest one. The standard-conforming approach is to write:

character(len=wl), parameter :: CLASSNAMES(1:Nobjects) =
(/character(len=wl)::"Cat","Rhino","Dolphin"/)

This is what the message is suggesting to you. (Note: You can also use
[] in place of (/ /).)
--
Steve Lionel
Developer Products Division
Intel Corporation
Merrimack, NH

For email address, replace "invalid" with "com"

User communities for Intel Software Development Products
http://software.intel.com/en-us/forums/
Intel Software Development Products Support
http://software.intel.com/sites/support/
My Fortran blog
http://www.intel.com/software/drfortran

Refer to http://software.intel.com/en-us/articles/optimization-notice
for more information regarding performance and optimization choices in
Intel software products.

FortranFan

unread,
Apr 6, 2015, 3:13:08 PM4/6/15
to
Not sure if your version of Intel Fortran compiler supports it (Intel's latest version definitely does), but the current Fortran standard allows you to declare the named constant as

character(len=*), parameter :: CLASSNAMES(Nobjects) =
[ character(len=wl) :: "Cat", "Rhino", "Dolphin" ]


michael...@compuserve.com

unread,
Apr 6, 2015, 3:16:42 PM4/6/15
to
On Monday, 6 April 2015 20:46:29 UTC+2, Lorents wrote:
> I would like to initialize an array of type character using an array
> constructor as exemplified by the following code:
>
> program mytest
> integer, parameter :: wl=30
> integer, parameter :: Nobjects =3
> character(len=wl), parameter :: CLASSNAMES(1:Nobjects) =
> (/"Cat","Rhino","Dolphin"/)
>

You have simply hit a tiresome restriction that existed in Fortran 95 but that has been relaxed in Fortran 2003.

Regards,

Mike Mertcalf

Richard Maine

unread,
Apr 6, 2015, 3:21:39 PM4/6/15
to
Lorents <aq...@ehjs.com> wrote:
...
> character(len=wl), parameter :: CLASSNAMES(1:Nobjects) =
> (/"Cat","Rhino","Dolphin"/)
...
> but when I specify '-stand f95' (check conformance to Fortran 95) I get
> the following warning:
>
> warning #8208: If type specification is omitted, each ac-value
> expression in the array constructor of type CHARACTER must have the
> same length type parameters.
>
> In fact adding spaces at the end of the string so that they all have the
> same length (e.g. "Cat ") gets rid of the warning; is there anything
> I can do to avoid the warning without having to do this padding by hand
> for all strings?

Not in f95. Yes, I agree it's a pain. But as of f2003...

> Besides, why the error message says 'If type specification is omitted'?
> Am I not specifying that the array is of type character?

That's referring to a feature of array constructors new to f2003. It is
a type specification as part of the array constructor syntax. Yes, you
have a type specifier for the array, but remember (or become informed,
as the case may be) that Fortran expressions have meaning independent of
context. In particular, the array constructor

(/"Cat","Rhino","Dolphin"/)

gets evaluated *WITHOUT* looking anywhere else in the statement for how
to interpret it. It has to stand on its own. And on its own, there is no
way to tell what character length you intended. There were a bunch of
attempts to make this sort of thing "just work", but every one of them
had problems with various esoteric cases. Your particular case isn't
esoteric, but a rule in the standard has to work for all cases.

This necessity for explicit blank padding in character array
constructors happens to be the first official interpretation question I
ever asked about Fortran, back circa 1990 before I was on the committee.
Hmm. Come to think of it, I think I posted the question here, but a
committee member took my posting and turned it into an official interp
request.

A way around it, as of f2003, is to put a type specification inside of
the array constructor. Yes, I know it looks redundant. But it does make
things work for *ALL* cases. Change the constructor to

[character(len=w):: "Cat", "Rhino", "Dolphin"]

Well, if you prefer, you can keep the (/../) delimitters instead of the
[..], but I strongly prefer the square brackets, so I wrote it that way.
That's also new to f2003.

--
Richard Maine
email: last name at domain . net
dimnain: summer-triangle

Steve Lionel

unread,
Apr 6, 2015, 3:43:26 PM4/6/15
to
I don't think so, Mike. F2003 does allow you to add a type-spec in the
constructor, as Richard notes, but without that the restriction remains:

F2003: "If type-spec is omitted, each ac-value expression in the array
constructor shall have the same length type parameters..." (p68, lines 8-10)

F2008 (after Interp F08/0080 in Corrigendum 2): "If type-spec is
omitted, corresponding length type parameters of the declared type of
each ac-value expression shall have the same value..." (p85, lines 13-14)

glen herrmannsfeldt

unread,
Apr 6, 2015, 4:37:42 PM4/6/15
to
Richard Maine <nos...@see.signature> wrote:

(snip)
> That's referring to a feature of array constructors new to f2003. It is
> a type specification as part of the array constructor syntax. Yes, you
> have a type specifier for the array, but remember (or become informed,
> as the case may be) that Fortran expressions have meaning independent of
> context. In particular, the array constructor

> (/"Cat","Rhino","Dolphin"/)

> gets evaluated *WITHOUT* looking anywhere else in the statement for how
> to interpret it. It has to stand on its own. And on its own, there is no
> way to tell what character length you intended. There were a bunch of
> attempts to make this sort of thing "just work", but every one of them
> had problems with various esoteric cases. Your particular case isn't
> esoteric, but a rule in the standard has to work for all cases.

And, as the OP already noted, different from DATA statements.

In the DATA statement case, the values on the right are initializing
the variables, and don't stand on their own.

-- glen

Richard Maine

unread,
Apr 6, 2015, 5:49:49 PM4/6/15
to
glen herrmannsfeldt <g...@ugcs.caltech.edu> wrote:

> Richard Maine <nos...@see.signature> wrote:
> > remember (or become informed,
> > as the case may be) that Fortran expressions have meaning independent of
> > context.... It has to stand on its own. And on its own, there is no
> > way to tell what character length you intended.
> And, as the OP already noted, different from DATA statements.
>
> In the DATA statement case, the values on the right are initializing
> the variables, and don't stand on their own.

I do not believe that to be an accurate. It appears to me that they have
to stand on their own just like anything else.

For example, it has been noted that in

double precision pi
data pi/3.14159265358979/

the literal is a single precision constant because its interpretation
stands on its own without reference to the type or kind of the variable
it is initializing.

You can get by without blank padding for character arrays in a DATA
statement, but that has nothing to do with the interpretation of the
data values depending on the context. That arises solely because each
data value is treated separately and initializes an individual array
element.

The syntax of the values in a DATA statement is strange (for example, in
that 2*3 does not equal 6), but I'm not off-hand aware of any way in
which the interpretation of the values does not have to stand on their
own. Yes, character values get truncated or blank padded, but that is
only after their initial interpretation on their own - just like
anywhere else.

Lorents

unread,
Apr 15, 2015, 6:09:45 AM4/15/15
to
On 06/04/15 20:12, Steve Lionel wrote:
[...]
>
> You are specifying that CLASSNAMES is type CHARACTER(len=wl) but not the
> array constructor. The standard requires that all values in an array
> constructor have the same type and type parameters. Length is a type
> parameter.
>
> Intel Fortran has an extension where it will pad the shorter values to
> the length of the longest one. The standard-conforming approach is to
> write:
>
> character(len=wl), parameter :: CLASSNAMES(1:Nobjects) =
> (/character(len=wl)::"Cat","Rhino","Dolphin"/)

Dear Steve,
thank you very much to you and the others for the comments, the
specification above works perfectly!

L.


0 new messages