# Character array constructor

**URL:** <https://fortran-lang.discourse.group/t/character-array-constructor/5591>\
**Category:** Uncategorized\
**Created:** [April 17, 2023, 4:10am UTC](https://fortran-lang.discourse.group/t/character-array-constructor/5591 "2023-04-17T04:10:45Z")\
**Posts on this page:** 7\
**Page:** 2

<div class="post-metadata">

**Author:** ![FortranFan](https://avatars.discourse-cdn.com/v4/letter/f/96bed5/32.png) [@FortranFan](https://fortran-lang.discourse.group/u/FortranFan)\
**Post date:** [April 18, 2023, 11:37pm UTC](https://fortran-lang.discourse.group/t/character-array-constructor/5591/24 "2023-04-18T23:37:24Z")

</div>

> [@FortranFan](#):
>
> The “canonical” form of the operation here is
> 
> ```auto
> allocate( tmp(new_shape) )
> tmp(..) = arr
> call move_alloc( from=tmp, to=arr )
> 
> ```

I did mean for the “canonical” form to apply to bounds as well:

```fortran
    integer, allocatable :: n(:), tmp(:)
    n = [1, 2, 3]
    print *, "lbound(n) = ", lbound(n, dim=1)
    allocate( tmp(-1:1) )
    tmp = n
    call move_alloc( from=tmp, to=n )
    print *, "lbound(n) following move_alloc: ", lbound(n, dim=1)
end

```

```auto
C:\temp>p.exe
 lbound(n) = 1
 lbound(n) following move_alloc: -1

```

---

<div class="post-metadata">

**Author:** ![FortranFan](https://avatars.discourse-cdn.com/v4/letter/f/96bed5/32.png) [@FortranFan](https://fortran-lang.discourse.group/u/FortranFan)\
**Post date:** [April 19, 2023, 12:12am UTC](https://fortran-lang.discourse.group/t/character-array-constructor/5591/25 "2023-04-19T00:12:04Z")

</div>

> [@msz59](#):
>
> how about a “dynamic” constructor, like in:  
> …  
> … ifort compile this code w/o any warning

Indeed , as has been mentioned upthread already, Intel Fortran supports what is desired by OP as an (unintended?) extension:

```fortran
   character(len=:), allocatable :: s(:)
   s = ["a", "bb", "ccc"]
   print *, s
end 

```

```auto
C:\temp>ifort /standard-semantics /free p.f
Intel(R) Fortran Intel(R) 64 Compiler Classic for applications running on Intel(R) 64, Version 2021.8.0 Build 20221119_000000
Copyright (C) 1985-2022 Intel Corporation. All rights reserved.

Microsoft (R) Incremental Linker Version 14.31.31105.0
Copyright (C) Microsoft Corporation. All rights reserved.

-out:p.exe
-subsystem:console
p.obj

C:\temp>p.exe
 a bb ccc

```

---

<div class="post-metadata">

**Author:** ![sblionel](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/sblionel/32/853_2.png) [@sblionel](https://fortran-lang.discourse.group/u/sblionel)\
**Post date:** [April 19, 2023, 12:35am UTC](https://fortran-lang.discourse.group/t/character-array-constructor/5591/26 "2023-04-19T00:35:03Z")

</div>

> [@FortranFan](#):
>
> Indeed , as has been mentioned upthread already, Intel Fortran supports what is desired by OP as an (unintended?) extension:

Deliberate, not unintended.

---

<div class="post-metadata">

**Author:** ![Beliavsky](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@Beliavsky](https://fortran-lang.discourse.group/u/Beliavsky)\
**Post date:** [April 19, 2023, 1:09pm UTC](https://fortran-lang.discourse.group/t/character-array-constructor/5591/27 "2023-04-19T13:09:09Z")

</div>

> [@VladimirF](#):
>
> It needs to be known somehow. The Fortran standard committee decided that it needs to be specified explicitly, but that is not that only way how the length could be determined in other theoretical alternative possibilities. The compiler could go through all of the elements, at least in constant expressions, and determine the length of the longest element. Still we have to consider that it would have to be able to deal with named constants, function calls, operators and so it would not be simple parsing, but fixed uniform size does not necesarilly require explicit size specification.

A [comment](https://github.com/j3-fortran/fortran_proposals/issues/301#issuecomment-1511697213) at GitHub on the issue by Peter Klausler said similar things:

> As complete implementations of the language must already be capable of evaluating every effective item in an array constructor into an expanding pool of dynamic memory in worst case scenarios – think about an array constructor with implied DO loops with dynamic bounds, or items that are function results of dynamic shape – this request shouldn’t require too much extra work in a compiler or its runtime support library.

If the LEN of the character constructor was set to the largest LEN of elements in the constructor known at compile time – literals and PARAMETERs – I think that would provide the functionality most people want. Ideally compilers would have an option to warn at run-time if truncation occurred in a character constructor.

---

<div class="post-metadata">

**Author:** ![RonShepard](https://avatars.discourse-cdn.com/v4/letter/r/a3d4f5/32.png) [@RonShepard](https://fortran-lang.discourse.group/u/RonShepard)\
**Post date:** [April 19, 2023, 7:47pm UTC](https://fortran-lang.discourse.group/t/character-array-constructor/5591/28 "2023-04-19T19:47:50Z")

</div>

> [@FortranFan](#):
>
> I did mean for the “canonical” form to apply to bounds as well:
> 
> ```auto
> integer, allocatable :: n(:), tmp(:)
> n = [1, 2, 3]
> print *, "lbound(n) = ", lbound(n, dim=1)
> allocate( tmp(-1:1) )
> tmp = n
> call move_alloc( from=tmp, to=n )
> print *, "lbound(n) following move_alloc: ", lbound(n, dim=1)
> end
> 
> ```

I think this is one of those cases where the standard is not clear. If it were written as `tmp(:)=n`, then no reallocation should occur and the lower bound should remain -1. But written as `tmp=n`, the standard allows reallocation, in which case the lower bound afterwards would be 1, not -1.

---

<div class="post-metadata">

**Author:** ![Harper](https://avatars.discourse-cdn.com/v4/letter/h/b5ac83/32.png) [@Harper](https://fortran-lang.discourse.group/u/Harper)\
**Post date:** [May 20, 2023, 6:02am UTC](https://fortran-lang.discourse.group/t/character-array-constructor/5591/29 "2023-05-20T06:02:04Z")

</div>

Sorrty to raise the matter of jagged character array constructors again but @sblionel told us that Intel Fortran supports them. Perhaps they could be allowed in f202y. I don’t use them now because I usually try as many compilers as I can when testing a new program.

---

<div class="post-metadata">

**Author:** ![sblionel](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/sblionel/32/853_2.png) [@sblionel](https://fortran-lang.discourse.group/u/sblionel)\
**Post date:** [May 21, 2023, 11:44pm UTC](https://fortran-lang.discourse.group/t/character-array-constructor/5591/30 "2023-05-21T23:44:29Z")

</div>

Unlikely, as the standards committee already considered this and rejected it in favor of the current ability to specify the common length.

[Previous page](https://fortran-lang.discourse.group/t/character-array-constructor/5591.md?page=1)
