# Error: Rank mismatch in argument ‘cars’ at (1) (scalar and rank-1)

**URL:** <https://fortran-lang.discourse.group/t/error-rank-mismatch-in-argument-cars-at-1-scalar-and-rank-1/3062>\
**Category:** Help\
**Created:** [March 28, 2022, 6:16pm UTC](https://fortran-lang.discourse.group/t/error-rank-mismatch-in-argument-cars-at-1-scalar-and-rank-1/3062 "2022-03-28T18:16:23Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![giraffe](https://avatars.discourse-cdn.com/v4/letter/g/f1d935/32.png) [@giraffe](https://fortran-lang.discourse.group/u/giraffe)\
**Post date:** [March 28, 2022, 6:16pm UTC](https://fortran-lang.discourse.group/t/error-rank-mismatch-in-argument-cars-at-1-scalar-and-rank-1/3062/1 "2022-03-28T18:16:23Z")

</div>

With these compiler options, how do I prevent this error and maintain the logic in the program?

`-g -Wall -Werror -fmax-errors=1 -fcheck=all`

```auto
SUBROUTINE TESTING (cars)
CHARACTER cars*(*)
print *,"CARS=", cars(1),cars(2),cars(3)
end 
Program TEST
Character test1 * 10
Character test2 * 30
COMMON /ABC/test1(10)
COMMON /DEF/test2(30)
test1(1)= 'A'
test1(2)= 'B'
test1(3)= 'C'
CALL TESTING (test1)

test2(1)= 'X'
test2(2)= 'Y'
test2(3)= 'Z'
CALL TESTING (test2)
End Program TEST

```

```auto
Error Message:
   13 | CALL TESTING (test1)
      | 1
Error: Rank mismatch in argument ‘cars’ at (1) (scalar and rank-1) [-Werror=argument-mismatch]
f951: all warnings being treated as errors

```

---

<div class="post-metadata">

**Author:** ![alozada](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/alozada/32/298_2.png) [@alozada](https://fortran-lang.discourse.group/u/alozada)\
**Post date:** [March 28, 2022, 6:49pm UTC](https://fortran-lang.discourse.group/t/error-rank-mismatch-in-argument-cars-at-1-scalar-and-rank-1/3062/2 "2022-03-28T18:49:00Z")

</div>

With some changes seems to work, 🙂

```auto
module testing_mod 
  implicit none

  character(len=10) :: test1(10)
  character(len=30) :: test2(30) 

contains
 
   subroutine testing_sub (cars)
      character(*) :: cars(:)
      print *,"CARS=", cars(1),cars(2),cars(3)
   end subroutine testing_sub
  
end module testing_mod
 
program test
  use testing_mod
   
   test1(1)= "A"
   test1(2)= "B"
   test1(3)= "C"
   call testing_sub(test1)
  
   test2(1)= "X"
   test2(2)= "Y"
   test2(3)= "Z"
   call testing_sub(test2)
  
end program test 

``
gfortran -g -Wall -Werror -fmax-errors=1 -fcheck=all rank.f90 -o rank

./rank
 CARS=A B C         
 CARS=X Y Z
```

---

<div class="post-metadata">

**Author:** ![alozada](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/alozada/32/298_2.png) [@alozada](https://fortran-lang.discourse.group/u/alozada)\
**Post date:** [March 28, 2022, 7:24pm UTC](https://fortran-lang.discourse.group/t/error-rank-mismatch-in-argument-cars-at-1-scalar-and-rank-1/3062/4 "2022-03-28T19:24:01Z")

</div>

The `if` clause requires a scalar logical expression

you could use something like:

```auto
do i = 1, n  
  if (cars(i)(1:1) == '<' ) print*, "works"
end do
```

---

<div class="post-metadata">

**Author:** ![msz59](https://avatars.discourse-cdn.com/v4/letter/m/3d9bf3/32.png) [@msz59](https://fortran-lang.discourse.group/u/msz59)\
**Post date:** [March 28, 2022, 8:32pm UTC](https://fortran-lang.discourse.group/t/error-rank-mismatch-in-argument-cars-at-1-scalar-and-rank-1/3062/5 "2022-03-28T20:32:31Z")

</div>

Maybe it is worth explaining the problem with original source, apparently Fortran 77 standard, as there are several errors in it.

1. `CHARACTER cars*(*)` tells the compiler that the dummy argument `cars` will be an object of type _assumed length character scalar_, not an array! It is F77 way of modern `CHARACTER(LEN=*)`. Furthermore, the nature of this object is deduced by the compiler only when it finds it in context, here the context is `print *,"CARS=",cars(1)...`. As `cars` is a scalar, not an array, `cars(1)` is interpreted as a function call, thus making the `cars` a dummy procedure!
2. In the main program, `test1` and `test2` are defined as arrays (1-dimensional, a.k.a. rank-1) of character elements having the length of 10 and 30, respectively. This is why you get `Rank mismatch.. (scalar and rank-1)` at `CALL TESTING()` statement.

So, to keep the old format, you’d have to change the dummy declaration to  
`CHARACTER*(*) cars(*)`  
to make it an _array of assumed length character elements_

To get convinced about the point 1 above regarding the _dummy procedure_, change (in the original code!) the `test1` and `test2` in `CALL TESTING()` to any scalar character value, say `"abc"` and `"def"` and compile. You’ll get something like:  
`Error: Expected a procedure for argument 'cars'`

Surely @alozada’s solution is much better, converted to modern Fortran.

---

<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:** [March 29, 2022, 6:11am UTC](https://fortran-lang.discourse.group/t/error-rank-mismatch-in-argument-cars-at-1-scalar-and-rank-1/3062/6 "2022-03-29T06:11:43Z")

</div>

> [@msz59](#):
>
> Surely @alozada’s solution is much better, converted to modern Fortran.

An implicit none would be nice. Goes to show how easy it can be to forget.

---

<div class="post-metadata">

**Author:** ![alozada](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/alozada/32/298_2.png) [@alozada](https://fortran-lang.discourse.group/u/alozada)\
**Post date:** [March 29, 2022, 7:17am UTC](https://fortran-lang.discourse.group/t/error-rank-mismatch-in-argument-cars-at-1-scalar-and-rank-1/3062/7 "2022-03-29T07:17:20Z")

</div>

> [@ivanpribec](#):
>
> An implicit none would be nice.

of course, 😳 . `implicit none`

---

<div class="post-metadata">

**Author:** ![msz59](https://avatars.discourse-cdn.com/v4/letter/m/3d9bf3/32.png) [@msz59](https://fortran-lang.discourse.group/u/msz59)\
**Post date:** [March 29, 2022, 3:06pm UTC](https://fortran-lang.discourse.group/t/error-rank-mismatch-in-argument-cars-at-1-scalar-and-rank-1/3062/9 "2022-03-29T15:06:45Z")

</div>

> [@giraffe](#):
>
> How do you get ‘B’, ‘C’, ‘Y’, and ‘Z’ to be output?

By reading and applying my earlier hint?

> [@msz59](#):
>
> So, to keep the old format, you’d have to change the dummy declaration to  
> `CHARACTER*(*) cars(*)`

As it is now, without the magical `*(*)` , `cars` dummy argument is an array of len=1 character elements. And you are sending arrays of len=10 or len=30 elements
