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

Q: Can I allocate a variable length character string?

106 views
Skip to first unread message

James Tursa

unread,
Jul 25, 2007, 11:00:38 PM7/25/07
to

I am trying to do the following (or something like this):

character(len=*), allocatable :: s
integer n
n = (some code here to determine how large the string needs to be)
allocate(s*(n))

But I am getting compiler errors, "... can only allocate arrays ...",
etc.

Is it possible to allocate a string of variable length?

I know I can do the following:

character, allocatable :: s(:)
integer n
n = (some code here to determine how large the string needs to be)
allocate(s(n))

But that gives me an array of single characters. I want a single
character string of length n, not an array of size n of single
characters.

Can this be done? And what is the syntax?

robert....@sun.com

unread,
Jul 26, 2007, 12:32:44 AM7/26/07
to


It can be done in Fortran 2003. The syntax is

CHARACTER(LEN=*), ALLOCATABLE :: S

Bob Corbett

robert....@sun.com

unread,
Jul 26, 2007, 12:44:43 AM7/26/07
to
On Jul 25, 9:32 pm, robert.corb...@sun.com wrote:
>
> It can be done in Fortran 2003. The syntax is
>
> CHARACTER(LEN=*), ALLOCATABLE :: S
>
> Bob Corbett

Oooops. It should be

CHARACTER(LEN=:), ALLOCATABLE :: S

Bob Corbett

Richard Maine

unread,
Jul 26, 2007, 1:12:06 AM7/26/07
to
<robert....@sun.com> wrote:

> On Jul 25, 9:32 pm, robert.corb...@sun.com wrote:
> >
> > It can be done in Fortran 2003. The syntax is

> Oooops. It should be
>
> CHARACTER(LEN=:), ALLOCATABLE :: S

And I note that most f95 compilers, even ones that have quite a few of
the f2003 features, do not seem to implement this one yet. For example,
NAG doesn't do it.

I'm personally anxious for this one. It tops my list of f2003 features
that I wish vendors would get done. There were a few other things on my
list (such as C interop), but several vendors seem to have that one.
Allocatable scalars have been slow in comming.

There are two reasons I'm "hot" for this:

1. I think this is the "right" way to do variable-length characters.
People fiddled around with several proposals for many years, but I think
this one is better than all the prior proposals that I saw.

2. Pretty much any serious use of the f2003 object oriented features
needs allocatable scalars. NAG has had the basic object oriented stuff
for several years now, but I find that I can't use it without
allocatab;e scalars. (Well, I *could* do workarounds, but they are bad
enough that they destroy the whole point in my opinion).

--
Richard Maine | Good judgement comes from experience;
email: last name at domain . net | experience comes from bad judgement.
domain: summertriangle | -- Mark Twain

robert....@sun.com

unread,
Jul 26, 2007, 1:53:53 AM7/26/07
to
On Jul 25, 10:12 pm, nos...@see.signature (Richard Maine) wrote:

> <robert.corb...@sun.com> wrote:
> > On Jul 25, 9:32 pm, robert.corb...@sun.com wrote:
>
> > > It can be done in Fortran 2003. The syntax is
> > Oooops. It should be
>
> > CHARACTER(LEN=:), ALLOCATABLE :: S
>
> And I note that most f95 compilers, even ones that have quite a few of
> the f2003 features, do not seem to implement this one yet. For example,
> NAG doesn't do it.
>
> I'm personally anxious for this one. It tops my list of f2003 features
> that I wish vendors would get done. There were a few other things on my
> list (such as C interop), but several vendors seem to have that one.
> Allocatable scalars have been slow in coming.

Most allocatable scalars are easy to implement, but are pointless.
Deferred length CHARACTER types are useful, but require quite a bit
of work to implement.

Bob Corbett
work to implement.

robert....@sun.com

unread,
Jul 26, 2007, 1:57:58 AM7/26/07
to
On Jul 25, 10:53 pm, robert.corb...@sun.com wrote:
>
>
> Most allocatable scalars are easy to implement, but are pointless.

I have been having trouble with much of my posting recently.
I meant to say it is pointless in the absence of other Fortran 2003
features that are hard to implement. Deferred-length CHARACTERs
are useful by themselves, but they are hard to implement.

Bob Corbett

gary.l...@lmco.com

unread,
Jul 26, 2007, 10:22:16 AM7/26/07
to
On Jul 26, 12:12 am, nos...@see.signature (Richard Maine) wrote:

> <robert.corb...@sun.com> wrote:
> > On Jul 25, 9:32 pm, robert.corb...@sun.com wrote:
>
> > > It can be done in Fortran 2003. The syntax is
> > Oooops. It should be
>
> > CHARACTER(LEN=:), ALLOCATABLE :: S
>
> And I note that most f95 compilers, even ones that have quite a few of
> the f2003 features, do not seem to implement this one yet. For example,
> NAG doesn't do it.
>
> I'm personally anxious for this one. It tops my list of f2003 features
> that I wish vendors would get done. There were a few other things on my
> list (such as C interop), but several vendors seem to have that one.
> Allocatable scalars have been slow in comming.
>
> There are two reasons I'm "hot" for this:
>
> 1. I think this is the "right" way to do variable-length characters.

Well, I don't like to think of variable length strings as
allocatable. I'd just rather specify them as:

character, variable :: mystring !variable length, no maximum
specified, implementation dependent maximum (or varlen/fixlen or some
other similar key word)

or

character(256), variable :: mystring !variable but maximum of 256

or

character(256), fixed :: mystring !regular fixed

Specifying as allocatable rather than variable length seems to be
specifying a "low level detail", more so than just specifying
"variable".

0 new messages