# Fortran interface for a 2D array of C chars

**URL:** <https://fortran-lang.discourse.group/t/fortran-interface-for-a-2d-array-of-c-chars/3186>\
**Category:** Help\
**Created:** [April 8, 2022, 6:21pm UTC](https://fortran-lang.discourse.group/t/fortran-interface-for-a-2d-array-of-c-chars/3186 "2022-04-08T18:21:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![general\_rishkin](https://avatars.discourse-cdn.com/v4/letter/g/a9a28c/32.png) [@general\_rishkin](https://fortran-lang.discourse.group/u/general_rishkin)\
**Post date:** [April 8, 2022, 6:21pm UTC](https://fortran-lang.discourse.group/t/fortran-interface-for-a-2d-array-of-c-chars/3186/1 "2022-04-08T18:21:24Z")

</div>

Given the following:

> char filename[num1][num2];

What is the Fortran binding for this?

This:

> character(len=num2, kind=c\_char), dimension(num1) :: filename

gives the error:

`Component 'filename' of BIND(C) type at (1) must have length one`

---

<div class="post-metadata">

**Author:** ![han190](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/han190/32/7158_2.png) [@han190](https://fortran-lang.discourse.group/u/han190)\
**Post date:** [April 8, 2022, 6:57pm UTC](https://fortran-lang.discourse.group/t/fortran-interface-for-a-2d-array-of-c-chars/3186/2 "2022-04-08T18:57:50Z")

</div>

Hi, I guess that is a two dimensional array? If yes, then

```fortran
character(:), allocatable :: filename(:, :)
! I don't know what are you going to do with the array
! but Fortran is column major so we might save it as 
! (num2, num1) instead of (num1, num2).
allocate (character(len=50, kind=c_char) : filename(num2, num1))

```

where `len` represents the length of each array element.

---

<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:** [April 8, 2022, 7:13pm UTC](https://fortran-lang.discourse.group/t/fortran-interface-for-a-2d-array-of-c-chars/3186/3 "2022-04-08T19:13:18Z")

</div>

If you want it to be the same as thing as the C variable

```auto
character(len=1, kind=c_char), dimension(num2,num1), bind(C, name='filename') :: filename

```

if you just want it to be interoperable with C, just leave of the `bind(C...)` part

---

<div class="post-metadata">

**Author:** ![general\_rishkin](https://avatars.discourse-cdn.com/v4/letter/g/a9a28c/32.png) [@general\_rishkin](https://fortran-lang.discourse.group/u/general_rishkin)\
**Post date:** [April 8, 2022, 7:57pm UTC](https://fortran-lang.discourse.group/t/fortran-interface-for-a-2d-array-of-c-chars/3186/4 "2022-04-08T19:57:36Z")

</div>

Excellent. Thanks @everythingfunctional.  
Yes, indeed, I do need it to be interoperable with C.
