# How do i allocate an array of strings?

**URL:** https://fortran-lang.discourse.group/t/how-do-i-allocate-an-array-of-strings/3930
**Category:** Uncategorized
**Created:** [July 5, 2022, 5:12pm UTC](https://fortran-lang.discourse.group/t/how-do-i-allocate-an-array-of-strings/3930 "2022-07-05T17:12:13Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![yeti0904](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/yeti0904/32/1886_2.png) [@yeti0904](https://fortran-lang.discourse.group/u/yeti0904)
#### Post date: [July 5, 2022, 5:12pm UTC](https://fortran-lang.discourse.group/t/how-do-i-allocate-an-array-of-strings/3930/1 "2022-07-05T17:12:13Z")

</div>

i have tried just doing:

```auto
character (len = :), dimension(:), allocatable :: menuButtons
...
allocate(menuButtons(3))

```

but this errors with

```auto
Error: Allocate-object at (1) with a deferred type parameter requires either a type-spec or SOURCE tag or a MOLD tag

```

---

<div class="post-metadata">

### Author: ![rwmsu](https://avatars.discourse-cdn.com/v4/letter/r/48db29/32.png) [@rwmsu](https://fortran-lang.discourse.group/u/rwmsu)
#### Post date: [July 5, 2022, 5:29pm UTC](https://fortran-lang.discourse.group/t/how-do-i-allocate-an-array-of-strings/3930/2 "2022-07-05T17:29:17Z")

</div>

You can only allocate arrays of fixed length strings

ie.

```auto
Character(LEN=132), Allocatable :: menuButtons(:)

```

To get an array of deferred length (ie allocatable) strings you have to embed them  
in a derived type

```auto

Type DLstring_t
   Character(LEN=:), allocatable, :: aString
End Type

Type(DLstring_t), ALLOCATABLE :: DLstringArray(:)

Allocate(DLstringArray(50))
DLstringArray(1)%astring = "This is the first string"
DLstringArray(2)%astring = "Second string"

etc

```

---

<div class="post-metadata">

### Author: ![yeti0904](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/yeti0904/32/1886_2.png) [@yeti0904](https://fortran-lang.discourse.group/u/yeti0904)
#### Post date: [July 5, 2022, 5:36pm UTC](https://fortran-lang.discourse.group/t/how-do-i-allocate-an-array-of-strings/3930/3 "2022-07-05T17:36:42Z")

</div>

oh ok, thanks

---

<div class="post-metadata">

### Author: ![yeti0904](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/yeti0904/32/1886_2.png) [@yeti0904](https://fortran-lang.discourse.group/u/yeti0904)
#### Post date: [July 5, 2022, 6:00pm UTC](https://fortran-lang.discourse.group/t/how-do-i-allocate-an-array-of-strings/3930/4 "2022-07-05T18:00:57Z")

</div>

how do i index a string of a character array? this doesnt seem to work:

```auto
Menu_Init%menuButtons(i)(spaceIndex) = c_null_char

```

---

<div class="post-metadata">

### Author: ![rwmsu](https://avatars.discourse-cdn.com/v4/letter/r/48db29/32.png) [@rwmsu](https://fortran-lang.discourse.group/u/rwmsu)
#### Post date: [July 5, 2022, 6:19pm UTC](https://fortran-lang.discourse.group/t/how-do-i-allocate-an-array-of-strings/3930/5 "2022-07-05T18:19:07Z")

</div>

You only have to add a C null character if you are trying to pass strings to C. Fortran will know the length of the individual string components of the derived type. Just have to use the LEN or LEN\_TRIM intrinsic functions to get the length. I believe that the need to add the null character for C\_Interop will be removed in Fortran 2023 but don’t quote me on that.

---

<div class="post-metadata">

### Author: ![rwmsu](https://avatars.discourse-cdn.com/v4/letter/r/48db29/32.png) [@rwmsu](https://fortran-lang.discourse.group/u/rwmsu)
#### Post date: [July 5, 2022, 6:26pm UTC](https://fortran-lang.discourse.group/t/how-do-i-allocate-an-array-of-strings/3930/6 "2022-07-05T18:26:10Z")

</div>

As to your original question, you have to use the index in the embedded string name

ie

```auto
to add the c_null_char to the end of a string just append it
menu_Init%menuButtons(i)%pushMe = menu_init%menuButtons(i)%pushme//C_NULL_CHAR

To get to an individual character in a string you use the index notation
Print *,' character 3 = ', menu_init%menuButtons(i)%pushme(3:3)

```

---

<div class="post-metadata">

### Author: ![yeti0904](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/yeti0904/32/1886_2.png) [@yeti0904](https://fortran-lang.discourse.group/u/yeti0904)
#### Post date: [July 5, 2022, 6:29pm UTC](https://fortran-lang.discourse.group/t/how-do-i-allocate-an-array-of-strings/3930/7 "2022-07-05T18:29:05Z")

</div>

but menuButtons is an array of strings not an array of a derived type

---

<div class="post-metadata">

### Author: ![yeti0904](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/yeti0904/32/1886_2.png) [@yeti0904](https://fortran-lang.discourse.group/u/yeti0904)
#### Post date: [July 5, 2022, 6:37pm UTC](https://fortran-lang.discourse.group/t/how-do-i-allocate-an-array-of-strings/3930/8 "2022-07-05T18:37:26Z")

</div>

figured it out, i have to do this:

```auto
Menu_Init%menuButtons(i)(spaceIndex:spaceIndex) = c_null_char

```

---

<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: [July 5, 2022, 7:13pm UTC](https://fortran-lang.discourse.group/t/how-do-i-allocate-an-array-of-strings/3930/9 "2022-07-05T19:13:42Z")

</div>

@yeti0904 , @rwmsu :

The discussion above is not fully accurate.

The standard for Fortran does indeed support deferred-length allocatable `CHARACTER` intrinsic type that are allocatable arrays, the effective restriction is each element of character array must have the same allocated length.

Thus the above mentioned derived type becomes a solution when one seeks the so-called jagged arrays, that is where the length of each element needs to be different, say in a collection of color names “red”, “green”, “blue” where, say, you don’t want any blank padding

@yeti0904 , with your original post, if you can work with allocatable arrays of character type where each element must be or can be the same length with blank padding on elements of shorter relevant string data, then you can do so. For example, you can try the following:

```fortran
   character(len=:), allocatable :: s(:)
   s = [character(len=6) :: "abcd", "pq" , "uvwxyz"]
   print *, s
end

```

---

<div class="post-metadata">

### Author: ![rwmsu](https://avatars.discourse-cdn.com/v4/letter/r/48db29/32.png) [@rwmsu](https://fortran-lang.discourse.group/u/rwmsu)
#### Post date: [July 5, 2022, 8:06pm UTC](https://fortran-lang.discourse.group/t/how-do-i-allocate-an-array-of-strings/3930/10 "2022-07-05T20:06:41Z")

</div>

@FortranFan

Yes you are correct, you can also (now) do

```auto
character(:), allocatable :: strings(:)
allocate(character(LEN=132) :: strings(50))

```

The reason I didn’t mention it was that one compiler that shall remain nameless would gag on the above logic when deferred length strings were first introduced. Granted that was a while ago. For the things I use strings for, I prefer the jagged array approach so thats what I tend to recommend to people.

---

<div class="post-metadata">

### Author: ![freevryheid](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/freevryheid/32/357_2.png) [@freevryheid](https://fortran-lang.discourse.group/u/freevryheid)
#### Post date: [July 6, 2022, 7:15pm UTC](https://fortran-lang.discourse.group/t/how-do-i-allocate-an-array-of-strings/3930/11 "2022-07-06T19:15:36Z")

</div>

This was recently added to the quickstart tut:

> **[Arrays and strings - Fortran Programming Language](https://fortran-lang.org/learn/quickstart/arrays_strings#array-of-strings)**
>
> Fortran Programming Language

---

<div class="post-metadata">

### Author: ![gWyche](https://avatars.discourse-cdn.com/v4/letter/g/f6c823/32.png) [@gWyche](https://fortran-lang.discourse.group/u/gWyche)
#### Post date: [September 15, 2023, 3:54pm UTC](https://fortran-lang.discourse.group/t/how-do-i-allocate-an-array-of-strings/3930/12 "2023-09-15T15:54:21Z")

</div>

I have the same question as this topic, but a bit more narrow for which FortranFan provided some explanation. I followed the link to the tutorial at [fortran-lang.org](http://fortran-lang.org) that freevryheid supplied.

I see above the term “jagged array” used a couple times. I read on but found no “contextual clue” and the tutorial did not specifically add an example of “an allocatable array of strings” whose string length is not known at compile time. This does come up when wanting a copy of an array of strings.

The last piece of the tutorial part of “Arrays and strings” did not include an example which left me with a suspicion that it was either left out, or not possible, or my being clueless that my question’s answer should be sought in a different section.

However, I will attempt FortranFan’s “character(len=:), allocatable :: s(:)”

---

<div class="post-metadata">

### Author: ![darianboggs](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/darianboggs/32/2095_2.png) [@darianboggs](https://fortran-lang.discourse.group/u/darianboggs)
#### Post date: [September 21, 2023, 3:16am UTC](https://fortran-lang.discourse.group/t/how-do-i-allocate-an-array-of-strings/3930/13 "2023-09-21T03:16:56Z")

</div>

“Jagged array” refers to an array of dimension 2 or more. Imagine a two-dimensional array where the number of row varies by column. That is a jagged array.

In the case of a **one-dimensional** array of `character` values (string), the length of the `character` values is the second dimension. It is not a two-dimensional `character` array. More importantly, Fortran does not support jagged arrays. Nonetheless, you can create something much like a “jagged array” in Fortran by creating an array of a derived type, where the derived type values are arrays with different sizes or `character` values of different lengths.

So, in this discussion, posters are referring to such a `character` “jagged array”.

Why does this matter? Well, languages like Java, Python, and C support jagged arrays (really just arrays of arrays) closely. The indexing looks like a multidimensional array, but it is an array of arrays, in fact.

This distinction matters since memory addresses of a Fortran multidimensional array are contiguous, where the memory addresses of arrays of arrays are not necessarily contiguous. Accessing contiguous addresses is much faster, which is why true multidimensional Fortran arrays are still advantageous.

---

<div class="post-metadata">

### Author: ![shahmoradi](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/shahmoradi/32/3151_2.png) [@shahmoradi](https://fortran-lang.discourse.group/u/shahmoradi)
#### Post date: [September 21, 2023, 6:19am UTC](https://fortran-lang.discourse.group/t/how-do-i-allocate-an-array-of-strings/3930/14 "2023-09-21T06:19:01Z")

</div>

Intel and GNU compilers used to have two orthogonal bugs for deferred length allocatable array of strings as dummy argument (or function output, if i remember correctly). If I bypassed it for one compiler, it would not compile with the other. That was several years ago of course, either or both might have been fixed by now.

---

<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: [September 21, 2023, 1:29pm UTC](https://fortran-lang.discourse.group/t/how-do-i-allocate-an-array-of-strings/3930/15 "2023-09-21T13:29:16Z")

</div>

> [@gWyche](#):
>
> … I read on but found no “contextual clue” and the tutorial did not specifically add an example of “an allocatable array of strings” whose string length is not known at compile time. …

@gWyche,

First, you can look up online on "jagged array"s. Here’s a [**link**](https://en.wikipedia.org/wiki/Jagged_array) to a Wikipedia article on this that likely summarizes in one place what you already know but which may help you dig deeper into the topic, if you so choose.

The bottom-line is Fortran does not intrinsically support the concept, meaning in the context of arrays of `CHARACTER` intrinsic type - as I wrote above - “each element of character array must have the same allocated length.”

It is unclear from your post what pending questions or issues you have with Fortran and arrays of deferred-length (the one with `:` in the declaration) `CHARACTER` type. Did you try the example I provided above?

It may help you to make specific inquiries if you are running into any issues with your use of character strings in Fortran.

---

<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: [September 21, 2023, 3:34pm UTC](https://fortran-lang.discourse.group/t/how-do-i-allocate-an-array-of-strings/3930/16 "2023-09-21T15:34:30Z")

</div>

> [@FortranFan](#):
>
> The bottom-line is Fortran does not intrinsically support the concept, meaning in the context of arrays of `CHARACTER` intrinsic type - as I wrote above - “each element of character array must have the same allocated length.”

The only thing that would be required for fortran to “intrinsically” support the concept would be for an intrinsic derived type such as

```auto
type string_type
   character(:), allocatable :: s
end type string_type

```

to be defined. This could be either added as a new intrinsic type, always available, or it could be a derived type that is imported from, for example, iso\_fortran\_env or some other intrinsic module.

One might ask, if it is so easy for the programmer to define and use such a type, then why should it be included in the standard as a new intrinsic type? The reason is that when each programmer defines a derived type like this, say within a library, or within a set of interoperable modules, then it is problematic for those different libraries to work together. The programmer must copy and convert data from one library’s derived type to the other library’s derived type. If it is defined by the standard, then everyone would use the same intrinsic type and everything would be portable and interoperable. There might be a few other advantages too, such as the ability to do i/o directly on an array of jagged strings, rather than referencing them as components as is currently required. Another useful feature that could be added to the intrinsic type would be the ability to initialize the components as they are declared – it is currently not allowed to initialize any allocatable entity in fortran.

---

<div class="post-metadata">

### Author: ![everythingfunctional](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/everythingfunctional/32/176_2.png) [@everythingfunctional](https://fortran-lang.discourse.group/u/everythingfunctional)
#### Post date: [September 22, 2023, 1:28am UTC](https://fortran-lang.discourse.group/t/how-do-i-allocate-an-array-of-strings/3930/17 "2023-09-22T01:28:30Z")

</div>

I am, and have been for a while, in favor of pushing this spec over the finish line: [http://www.astro.wisc.edu/~townsend/resource/download/code/Fortran-ISO\_VARYING\_STRING.pdf](http://www.astro.wisc.edu/~townsend/resource/download/code/Fortran-ISO_VARYING_STRING.pdf)

It would also even enable this syntax to “just work” (most likely):

```auto
type(varying_string), parameter :: options(*) = &
    [type(varying_string) :: "strings", "with", "varying", "lengths"]

```

---

<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: [September 22, 2023, 3:39am UTC](https://fortran-lang.discourse.group/t/how-do-i-allocate-an-array-of-strings/3930/18 "2023-09-22T03:39:04Z")

</div>

> [@everythingfunctional](#):
>
> would also even enable this syntax to “just work” (most likely):
> 
> ```auto
> type(varying_string), parameter :: options(*) = &
> [type(varying_string) :: "strings", "with", "varying", "lengths"]
> 
> ```

The specs of the old `ISO VARYING STRING` are now outdated, not to mention that part (2) of the standard is now deleted, just like the `CoCo` preprocessor.

Reinstating that will be like forcing the mid-90s style BRICK size phones on certain kids when others have long moved on.

Additionally, as an intrinsic type, a Fortranner must demand freedom from `type(..)` enclosure in the declaration:

```fortran
string, parameter :: options(*) = [string :: "strings", ..]

```

---

<div class="post-metadata">

### Author: ![everythingfunctional](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/everythingfunctional/32/176_2.png) [@everythingfunctional](https://fortran-lang.discourse.group/u/everythingfunctional)
#### Post date: [September 22, 2023, 11:16am UTC](https://fortran-lang.discourse.group/t/how-do-i-allocate-an-array-of-strings/3930/19 "2023-09-22T11:16:47Z")

</div>

> [@FortranFan](#):
>
> The specs of the old `ISO VARYING STRING` are now outdated

Maybe a bit, but I don’t think there is anything that fundamentally invalidates the overall approach, it just needs some updates.

> [@FortranFan](#):
>
> Additionally, as an intrinsic type, a Fortranner must demand freedom from `type(..)` enclosure in the declaration

Why? `event_type`, `lock_type`, etc. require `type()`. What’s so bad about the extra 6 characters?

---

<div class="post-metadata">

### Author: ![epagone](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/epagone/32/252_2.png) [@epagone](https://fortran-lang.discourse.group/u/epagone)
#### Post date: [September 22, 2023, 12:46pm UTC](https://fortran-lang.discourse.group/t/how-do-i-allocate-an-array-of-strings/3930/20 "2023-09-22T12:46:00Z")

</div>

> [@everythingfunctional](#):
>
> What’s so bad about the extra 6 characters?

They make needlessly verbose an already verbose language (adding the `varying_` prefix makes the problem worse). IMO, it’s just bloat. Maybe even more importantly, it is confusing from a logical standpoint: why `string` should be different than `integer`? In all modern languages “varying strings” are first class data types, if I’m not wrong.

[Next page](https://fortran-lang.discourse.group/t/how-do-i-allocate-an-array-of-strings/3930.md?page=2)
