# Problem with array definition

**URL:** <https://fortran-lang.discourse.group/t/problem-with-array-definition/1163>\
**Category:** Help\
**Created:** [May 6, 2021, 5:57pm UTC](https://fortran-lang.discourse.group/t/problem-with-array-definition/1163 "2021-05-06T17:57:55Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![CConde](https://avatars.discourse-cdn.com/v4/letter/c/d6d6ee/32.png) [@CConde](https://fortran-lang.discourse.group/u/CConde)\
**Post date:** [May 6, 2021, 5:57pm UTC](https://fortran-lang.discourse.group/t/problem-with-array-definition/1163/1 "2021-05-06T17:57:55Z")

</div>

Dear friends,

Please, I would like to request your help with the following problem: I have to define a large array containing zeros which don’t fit in a single row due to I’m exceeding the maximun numbers of character per row. For example:

a = [0, 0, 0, 0, 0, 0, …, 0, &  
0, 0, 0, 0, … 0, 0]

However, the second row cannot start with a zero given that, when compiling, I get this error  
" Syntax error in array constructor at (1). Zero is not a valid statement label at (1)"

How can I overcome this problem?

Many thanks for the help.

---

<div class="post-metadata">

**Author:** ![ivanpribec](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/ivanpribec/32/3290_2.png) [@ivanpribec](https://fortran-lang.discourse.group/u/ivanpribec)\
**Post date:** [May 6, 2021, 6:11pm UTC](https://fortran-lang.discourse.group/t/problem-with-array-definition/1163/2 "2021-05-06T18:11:14Z")

</div>

How about:

```auto
integer, parameter :: n = 1000000
real :: a(n)
a = 0

```

---

<div class="post-metadata">

**Author:** ![vmagnin](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/vmagnin/32/28_2.png) [@vmagnin](https://fortran-lang.discourse.group/u/vmagnin)\
**Post date:** [May 6, 2021, 6:50pm UTC](https://fortran-lang.discourse.group/t/problem-with-array-definition/1163/3 "2021-05-06T18:50:37Z")

</div>

> [@CConde](#):
>
> a = [0, 0, 0, 0, 0, 0, …, 0, &  
> 0, 0, 0, 0, … 0, 0]

Although @ivanpribec solution is THE solution, note that the label problem could probably be avoided by putting an & at the start of the second line:

```fortran
a = [0, 0, 0, 0, 0, 0, …, 0, &
   & 0, 0, 0, 0, … 0, 0]

```

**Updated:** in fact, no. See the post by @themos downward: [Problem with array definition - #9 by themos](https://fortran-lang.discourse.group/t/problem-with-array-definition/1163/9)  
In free-form format, there is no error and no need to add an &. And in fixed-form, the trailing & is not accepted.

---

<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:** [May 6, 2021, 6:57pm UTC](https://fortran-lang.discourse.group/t/problem-with-array-definition/1163/4 "2021-05-06T18:57:11Z")

</div>

and if a is allocatable you can do

allocate(a(n), source=0)

---

<div class="post-metadata">

**Author:** ![CConde](https://avatars.discourse-cdn.com/v4/letter/c/d6d6ee/32.png) [@CConde](https://fortran-lang.discourse.group/u/CConde)\
**Post date:** [May 6, 2021, 7:10pm UTC](https://fortran-lang.discourse.group/t/problem-with-array-definition/1163/5 "2021-05-06T19:10:44Z")

</div>

@ivanpribec, @vmagnin, @rwmsu, many thanks for your help.

---

<div class="post-metadata">

**Author:** ![johnalx](https://avatars.discourse-cdn.com/v4/letter/j/b38774/32.png) [@johnalx](https://fortran-lang.discourse.group/u/johnalx)\
**Post date:** [May 6, 2021, 8:12pm UTC](https://fortran-lang.discourse.group/t/problem-with-array-definition/1163/6 "2021-05-06T20:12:34Z")

</div>

How about using a `data` statement

```
integer, parameter :: n = 20
real, dimension(n) :: a; data a/n*0.0/

print *,a

```

produces 20 values of `0.0`

If this is fixed format file, then you need to add a continuation character in column 6

```
! |<-- column 7
      a = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 
     &0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
```

---

<div class="post-metadata">

**Author:** ![nbehrnd](https://avatars.discourse-cdn.com/v4/letter/n/e0b2c6/32.png) [@nbehrnd](https://fortran-lang.discourse.group/u/nbehrnd)\
**Post date:** [May 6, 2021, 9:25pm UTC](https://fortran-lang.discourse.group/t/problem-with-array-definition/1163/7 "2021-05-06T21:25:27Z")

</div>

By the large number of 0-elements in the examples above, on one hand, and some prior exposure to Python’s [scripy](https://docs.scipy.org/doc/scipy/reference/index.html) library on the other I noticed Fortran seems equally to work with [_sparse matrices_](https://docs.scipy.org/doc/scipy/reference/sparse.html#example-1) (or, perhaps more suitable for the parlance of Fortran, a _sparse arrays_). One of the entries found is [this one](https://stackoverflow.com/questions/16493753/automated-sparse-matricies-in-fortran).

---

<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:** [May 6, 2021, 10:22pm UTC](https://fortran-lang.discourse.group/t/problem-with-array-definition/1163/8 "2021-05-06T22:22:28Z")

</div>

> [@johnalx](#):
>
> How about using a `data` statement …

@johnalx ,

Welcome to the forum.

Re: the “data statement,” note the use of `DATA` also imparts the `SAVE` attribute to the variable, essentially making it a static variable and adversely impacting thread-safety. Coders may not want that attribute in modern applications.

An option to consider if `implied-do` is of interest is

```Fortran
a = [( xx, integer :: i = 1, n )] !<-- xx can be a constant such as zero, or a valid expression involving `i`

```

```auto

```

---

<div class="post-metadata">

**Author:** ![themos](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/themos/32/521_2.png) [@themos](https://fortran-lang.discourse.group/u/themos)\
**Post date:** [May 7, 2021, 10:18am UTC](https://fortran-lang.discourse.group/t/problem-with-array-definition/1163/9 "2021-05-07T10:18:25Z")

</div>

Hello,  
I am guessing you are using the gfortran compiler and you wrote something like this and saved it in a file foo.f.

```
c234567
      dimension a(6)
      a=[0,0,0, &
 0 ,0,0]
      end

```

What went wrong here is that you used free-format Fortran but the conventions of gfortran treat the contents of foo.f as fixed-form Fortran. In fixed-form the trailing ampersand (&) is an error (which is the _Error: Syntax error in array constructor at (1)_) and that first zero on line 3 is interpreted as a statement label and not part of a statement (which is the _Zero is not a valid statement label at (1)_)

If you write free-format Fortran you must either put it in a file with a filename extension that the compiler expects (most compilers will accept .f90 for this) or override the compiler’s defaults with a command line option (in gfortran’s case, _-ffree-form_)

---

<div class="post-metadata">

**Author:** ![johnalx](https://avatars.discourse-cdn.com/v4/letter/j/b38774/32.png) [@johnalx](https://fortran-lang.discourse.group/u/johnalx)\
**Post date:** [May 7, 2021, 12:41pm UTC](https://fortran-lang.discourse.group/t/problem-with-array-definition/1163/10 "2021-05-07T12:41:23Z")

</div>

I did not know that about the `data` statement. Thank you this caveat.

---

<div class="post-metadata">

**Author:** ![ivanpribec](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/ivanpribec/32/3290_2.png) [@ivanpribec](https://fortran-lang.discourse.group/u/ivanpribec)\
**Post date:** [May 7, 2021, 1:31pm UTC](https://fortran-lang.discourse.group/t/problem-with-array-definition/1163/11 "2021-05-07T13:31:24Z")

</div>

It is also possible to initialize the array with a constant:

```auto
module mymod
  implicit none
  integer, parameter :: n = 20
  real :: a(n) = 0 ! implicit save
end module
program main
use mymod
print *, a
end program

```

Edit: for any new Fortran programmers, the following might be more illustrating of what implicit save means

```nohighlight
module mymod
  implicit none
  real :: a(5) = 0 ! implicit save
end module
module usemymod
  use mymod, only: a
  implicit none
  private
  public :: modify_a
contains
  subroutine modify_a()
    a(1) = 42.
  end subroutine
end module
program main
  use usemymod, only: modify_a
  call modify_a
  block
    use mymod, only: a
    print *, a ! a(1) equals 42.
  end block
end program

```
