Re: bug in g95 with character arrays?
- From: nospam@xxxxxxxxxxxxx (Richard Maine)
- Date: Sat, 28 Jun 2008 08:54:52 -0700
wim <wim.van.hoydonck@xxxxxxxxx> wrote:
character(len=5), parameter :: test(5) =
["tan","atan2","atan","sin","asin"]
1
Error: Element in character array constructor at (1) has length 5
instead of 3
Still, this is a bug i think....
No, this is not a bug in g95. It is a bug in your code. This was
essentially the first interpretation question I ever asked about f90. I
read the standard and had trouble imagining that things would really
this way because it was so much of a PITA - shades of Pascal - so I
posted a question about it here. This was before I had ever been to a
standards meeting, much less been a member. Then when I went to my first
X3J3 meeting, I found that my question from the newsgroup had been
brought to X3J3 as a formal interpretation question.
It turns out that there are more complications to the question than I
had imagined. The complications arise in cases where it is non-trivial
for the compiler to tell up front to tell what the character lengths
are. Yes, that can happen. It usually involves things like function
calls where the function returns a string of computed length. Implied DO
loops that can possibly be zero-trip ones make things worse. Trust me
that it can get really, really messy - messier than I'd have ever
believed. Henry Zongaro had an incredible ability to find the obscure
cases that broke every rule that anyone proposed to "fix" things. The
messy cases mostly weren't things that "sane" users would actually do,
but they were a big deal for compiler writers who are supposed to make
the compiler work even for the "insane" users if the standard says it is
supposed to work.
It was in 1991 that I posed this question. Though it was formally
answered a few years later (yes, it took a few years because of all the
proposed answers that Henry's examples shot down), it isn't until f2003
that the PITA is "reasonably" fixed in my opinion. I was glad to read
here just a day or two ago that Gfortran actually implements that f2003
feature now. I haven't checked for it in g95; suppose I ought to.
In f2003, you can explicitly specify the type and type parameters for an
array constructor. This has several effects, one of which is that the
elements in the constructor can have diferent types and type parameters
as long as the needed intrinsic conversion exists.
Thus your example can be written in f2003 as
character(len=5), parameter :: test(5) = &
[character(len=5) :: "tan","atan2","atan","sin","asin"]
Although that looks a bit redundant, it does the job. The apparent
redundancy is because the array constructor does not pick up its type
and type parameters from context. See many, many questions about the
more general case of that principle (most often relating to precision of
reals).
--
Richard Maine | Good judgement comes from experience;
email: last name at domain . net | experience comes from bad judgement.
domain: summertriangle | -- Mark Twain
.
- Follow-Ups:
- Re: bug in g95 with character arrays?
- From: John Harper
- Re: bug in g95 with character arrays?
- From: glen herrmannsfeldt
- Re: bug in g95 with character arrays?
- From: wim
- Re: bug in g95 with character arrays?
- References:
- bug in g95 with character arrays?
- From: wim
- bug in g95 with character arrays?
- Prev by Date: Re: Is it time to legitimise REAL*8 etc?
- Next by Date: Re: Is it time to legitimise REAL*8 etc?
- Previous by thread: Re: bug in g95 with character arrays?
- Next by thread: Re: bug in g95 with character arrays?
- Index(es):