# Shape and Reshape Command

**URL:** <https://fortran-lang.discourse.group/t/shape-and-reshape-command/6491>\
**Category:** Help\
**Created:** [September 9, 2023, 7:33am UTC](https://fortran-lang.discourse.group/t/shape-and-reshape-command/6491 "2023-09-09T07:33:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sihaqqi](https://avatars.discourse-cdn.com/v4/letter/s/a183cd/32.png) [@sihaqqi](https://fortran-lang.discourse.group/u/sihaqqi)\
**Post date:** [September 9, 2023, 7:33am UTC](https://fortran-lang.discourse.group/t/shape-and-reshape-command/6491/1 "2023-09-09T07:33:37Z")

</div>

Greeting, I cannot understand RESHAPE and SHAPE command use in the program at

> **[RESHAPE - The GNU Fortran Compiler](https://gcc.gnu.org/onlinedocs/gcc-4.2.4/gfortran/RESHAPE.html#RESHAPE)**
>
> The GNU Fortran Compiler

PROGRAM test\_reshape  
INTEGER, DIMENSION(4) :: x  
WRITE(_,_) SHAPE(x) ! prints “4”  
WRITE(_,_) SHAPE(RESHAPE(x, (/2, 2/))) ! prints “2 2”  
END PROGRAM

When I just use WRITE(_,_) (RESHAPE(x, (/2, 2/))); result is garbage.

Can someone please provide me guidance as how RESHAPE command works in this program and how SHAPE operates on the complete statement in the last line.

Thanks in advance for guidance

---

<div class="post-metadata">

**Author:** ![snano](https://avatars.discourse-cdn.com/v4/letter/s/fbc32d/32.png) [@snano](https://fortran-lang.discourse.group/u/snano)\
**Post date:** [September 9, 2023, 7:43am UTC](https://fortran-lang.discourse.group/t/shape-and-reshape-command/6491/2 "2023-09-09T07:43:41Z")

</div>

try this

```auto
PROGRAM test_reshape
   INTEGER, DIMENSION(4) :: x
   WRITE (*,*) SHAPE(x) ! prints “4”
   WRITE (*,*) SHAPE(RESHAPE(x, (/2, 2/))) ! prints “2 2”
END PROGRAM

```

---

<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 9, 2023, 7:50am UTC](https://fortran-lang.discourse.group/t/shape-and-reshape-command/6491/3 "2023-09-09T07:50:32Z")

</div>

> [@sihaqqi](#):
>
> Can someone please provide me guidance as how RESHAPE command works in this program and how SHAPE operates on the complete statement in the last line.

The `RESHAPE()` intrinsic returns a two dimensional array in your example. The `SHAPE()` intrinsic returns the extents of the one dimensional array in the first statement and of the two dimensional array in the second statement. When you print out the 2D array, the result is garbage because the array `x(:)` was never defined – it was declared, but values were never given to the elements. The `SHAPE()` intrinsic works anyway because it does not depend on the values of the array elements, just on the array metadata (i.e. the rank and the extents).
