# How to implement in iso\_c\_binding

**URL:** <https://fortran-lang.discourse.group/t/how-to-implement-in-iso-c-binding/4293>\
**Category:** Uncategorized\
**Created:** [September 9, 2022, 10:12pm UTC](https://fortran-lang.discourse.group/t/how-to-implement-in-iso-c-binding/4293 "2022-09-09T22:12:39Z")\
**Posts on this page:** 13\
**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:** [September 9, 2022, 10:12pm UTC](https://fortran-lang.discourse.group/t/how-to-implement-in-iso-c-binding/4293/1 "2022-09-09T22:12:39Z")

</div>

```auto
struct myfloat 
{
   float float_tmp;
};

struct myint 
{
   int int_tmp;
};

int game_(myint1,myfloat2)
myint **myint;
myfloat **myfloat2;
{
  return(0);
}

```

---

<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:** [September 9, 2022, 11:31pm UTC](https://fortran-lang.discourse.group/t/how-to-implement-in-iso-c-binding/4293/2 "2022-09-09T23:31:04Z")

</div>

you could see some examples [here](https://fortran-lang.org/en/learn/quickstart/derived_types/?highlight=interoperability).

---

<div class="post-metadata">

**Author:** ![awvwgk](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/awvwgk/32/154_2.png) [@awvwgk](https://fortran-lang.discourse.group/u/awvwgk)\
**Post date:** [September 10, 2022, 12:00am UTC](https://fortran-lang.discourse.group/t/how-to-implement-in-iso-c-binding/4293/3 "2022-09-10T00:00:33Z")

</div>

Could you please clarify what you want to archive with this C code snippet? Without further information it will be difficult to give you any useful answer.

---

<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 10, 2022, 2:42am UTC](https://fortran-lang.discourse.group/t/how-to-implement-in-iso-c-binding/4293/4 "2022-09-10T02:42:24Z")

</div>

Given that isn’t actually valid C code, and you aren’t telling us what it’s for or what the goal is, this is close(ish) to the given example C code, and an example of calling it.

```c
typedef struct mf {
    float float_tmp;
} myfloat;

typedef struct mi {
    int int_tmp;
} myint;

int game(myint x, myfloat y) {
    return 42;
}

```

```fortran
program main
    use iso_c_binding, only: c_int, c_float

    implicit none

    type, bind(c) :: myfloat
        real(c_float) :: float_tmp
    end type

    type, bind(c) :: myint
        integer(c_int) :: int_tmp
    end type

    interface
        function game(x, y) bind(c, name="game")
            import :: myfloat, myint, c_int
            implicit none
            type(myint), intent(in), value :: x
            type(myfloat), intent(in), value :: y
            integer(c_int) :: game
        end function
    end interface

    type(myfloat) :: y
    type(myint) :: x

    print *, game(x, y)
end program

```

---

<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 12, 2022, 2:39pm UTC](https://fortran-lang.discourse.group/t/how-to-implement-in-iso-c-binding/4293/6 "2022-09-12T14:39:22Z")

</div>

> [@giraffe](#):
>
> I’d like to know (with 100% certainty) how you would implement this code using ISO\_C\_BINDING

Then hire me to work on the project, send me an NDA, show me the actual code and tell me how you’d like to be able to interact with it. Because you still haven’t provided valid C code (the compiler errors are shown below), or told us what you want to be able to do with it from Fortran.

```plaintext
$ gcc -c stuff.c -o stuff.o (master)
stuff.c: In function ‘game_’:
stuff.c:12:1: error: unknown type name ‘myint’; use ‘struct’ keyword to refer to the type
   12 | myint **myint1;
      | ^ ~~~~
      | struct 
stuff.c:13:1: error: unknown type name ‘myfloat’; use ‘struct’ keyword to refer to the type
   13 | myfloat **myfloat2;
      | ^ ~~~~~~
      | struct 

```

---

<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 12, 2022, 3:50pm UTC](https://fortran-lang.discourse.group/t/how-to-implement-in-iso-c-binding/4293/9 "2022-09-12T15:50:06Z")

</div>

```fortran
    use, intrinsic :: iso_c_binding, only: c_int, c_float, c_ptr

    type, bind(c) :: myfloat
        real(c_float) :: float_tmp
    end type

    type, bind(c) :: myint
        integer(c_int) :: int_tmp
    end type

    interface
        function game(myint1, myfloat2) bind(c, name="game_")
            import :: c_int, c_ptr
            implicit none
            type(c_ptr) :: myint1
            type(c_ptr) :: myfloat2
            integer(c_int) :: game
        end function
    end interface

```

That’s the interface to that C code. Note that due to Fortran’s “pass by reference”, a pointer to the pointer `myint1` is what will be passed to C (as you suspected).

---

<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 21, 2022, 6:33pm UTC](https://fortran-lang.discourse.group/t/how-to-implement-in-iso-c-binding/4293/11 "2022-09-21T18:33:48Z")

</div>

> [@giraffe](#):
>
> What do I do?

Convince management to bring somebody on that knows what they’re doing?

I don’t mean to offend, but it would take me an hour to write a coherent and comprehensive description of why what you’ve done is wrong, what the right way to do it is, and why you’re going to have problems if you don’t do it the right way. And even with that, I’m not convinced you wouldn’t come back with several variations on the same question, effectively having this community trying to do your work for you without being compensated.

---

<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 22, 2022, 1:21pm UTC](https://fortran-lang.discourse.group/t/how-to-implement-in-iso-c-binding/4293/15 "2022-09-22T13:21:51Z")

</div>

When you write an expression like `cptr1+j-1` in fortran, what exactly do you think is the result? Do you think it is the same as the same expression in C where `cptr1` is a C pointer?

---

<div class="post-metadata">

**Author:** ![milancurcic](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/milancurcic/32/2_2.png) [@milancurcic](https://fortran-lang.discourse.group/u/milancurcic)\
**Post date:** [September 22, 2022, 4:19pm UTC](https://fortran-lang.discourse.group/t/how-to-implement-in-iso-c-binding/4293/19 "2022-09-22T16:19:02Z")

</div>

Friendly reminder that everyone is welcome to post questions and ask for help on Fortran Discourse as long as they’re not disrespectful or otherwise violating the CoC. It’s perfectly OK to not respond to the topic if you don’t want to help.

---

<div class="post-metadata">

**Author:** ![milancurcic](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/milancurcic/32/2_2.png) [@milancurcic](https://fortran-lang.discourse.group/u/milancurcic)\
**Post date:** [September 22, 2022, 5:27pm UTC](https://fortran-lang.discourse.group/t/how-to-implement-in-iso-c-binding/4293/21 "2022-09-22T17:27:31Z")

</div>

It’s only free consultancy if you choose to respond. Please use the flag feature if you think any post is inappropriate or somehow abusing the Fortran Discourse and we’ll review it. Otherwise, please ignore the posts if you don’t like them.

---

<div class="post-metadata">

**Author:** ![egio](https://avatars.discourse-cdn.com/v4/letter/e/dbc845/32.png) [@egio](https://fortran-lang.discourse.group/u/egio)\
**Post date:** [September 23, 2022, 9:29am UTC](https://fortran-lang.discourse.group/t/how-to-implement-in-iso-c-binding/4293/24 "2022-09-23T09:29:42Z")

</div>

There are several problem, first `integer*8` is not in the Fortran standard even though it is a common extension that means an integer variable stored in 8 bytes.

The compiler is telling you that you cannot pass an integer variable to an opaque type as `type(c_ptr)`.  
An eight byte integer variable, from the point of view of the Fortran processor, is different from an opaque type equivalent to a C pointer like `type(c_ptr)`. Like in C a `long` is different from a `void*` even if they were of the same length in bytes.

Now in some old Fortran program `integer*8` is used to store C pointers on the assumption that the compiler will support that extension, you are on a 64 bit machine, and you don’t want the compiler to make any check.

`ISO_C_BINDING` is far better as it helps you to restrict the places where you have to manually be sure that what is written in C has a correct correspondence in Fortran (in the interfaces of the C routines), letting the compiler to help you in checking that you are calling the C routines correctly. Moreover it is standard and it will work in old 32 bit machines and future strange machines as well.

Personally I always use `iso_c_binding`, for example when I need to call a Fortran routine from python through the ctypes, the Fortran routine will appear as a C routine and everything is fine.

---

<div class="post-metadata">

**Author:** ![egio](https://avatars.discourse-cdn.com/v4/letter/e/dbc845/32.png) [@egio](https://fortran-lang.discourse.group/u/egio)\
**Post date:** [September 27, 2022, 11:09am UTC](https://fortran-lang.discourse.group/t/how-to-implement-in-iso-c-binding/4293/27 "2022-09-27T11:09:43Z")

</div>

> [@giraffe](#):
>
> _ **I’m going to try to work around this solution by using temporary variables.** __**Result: One major problem that I have trying to implement this algorithm is that the things I pass to game\_() aren’t only `integer*8` variables. Another value is added to each of the `integer*8` variables and that result of addition is what actually gets sent to game\_(). I can’t add a number to myint\_ptr and pass that to game\_()**_

Basically you are saying that you have an array and you pass elements of this array to the routine “game” or that you get back an array of objects. The only meaning (in C) to add a number to an address is when you want to get the addresses of successive array elements.

If so, well, use an array.

C\_F\_POINTER has a shape argument, so you can access successive array elements (if allocated inside the C function), this is the only equivalent way to add a number to the “integer\*8” as far as I may understand.  
Or use an array of type(c\_ptr), and pass each element of the array to the “game” routine, if that is what you want to do instead.

Look also at C\_ASSOCIATED if it may help you.

---

<div class="post-metadata">

**Author:** ![certik](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/certik/32/4_2.png) [@certik](https://fortran-lang.discourse.group/u/certik)\
**Post date:** [October 2, 2022, 10:58pm UTC](https://fortran-lang.discourse.group/t/how-to-implement-in-iso-c-binding/4293/31 "2022-10-02T22:58:19Z")

</div>

As @milancurcic said, please do not respond to a post if you think it is not worth your time.

If you ask a question here, please be polite and respectful to people that try to respond to it. Also try to provide as many details as you can, the background and motivation of the question, to increase chances of getting good actionable answers.
