# Fortran Pointer as argument in a subroutine

**URL:** <https://fortran-lang.discourse.group/t/fortran-pointer-as-argument-in-a-subroutine/6380>\
**Category:** Help\
**Created:** [August 21, 2023, 2:00pm UTC](https://fortran-lang.discourse.group/t/fortran-pointer-as-argument-in-a-subroutine/6380 "2023-08-21T14:00:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rfournier](https://avatars.discourse-cdn.com/v4/letter/r/eb8c5e/32.png) [@rfournier](https://fortran-lang.discourse.group/u/rfournier)\
**Post date:** [August 21, 2023, 2:00pm UTC](https://fortran-lang.discourse.group/t/fortran-pointer-as-argument-in-a-subroutine/6380/1 "2023-08-21T14:00:18Z")

</div>

the program is quite simple :

```auto
!
subroutine print_array(a)
integer, dimension(*) :: a
print*,'print::loc(a)=',loc(a)
end subroutine print_array
!
program test
integer, target, dimension(10) :: ival
integer, pointer, dimension(:) :: ival_ptr
!
print*,'test::loc(ival)=',loc(ival)
!
ival_ptr=>ival
call print_array(ival_ptr)
!
end

```

* * *

## Version 1 - Contiguous memory -

> gfortran -fcheck=array-temps -Warray-temporaries main.F

main.F:12.23:  
call print\_array(ival\_ptr)  
1  
Warning: Creating array temporary at (1)

And during execution, I can see that there is no copy:

> ./a.out  
> test::loc(ival)= 140731006653952  
> print::loc(a)= 140731006653952

* * *

## Version 2 - Non contiguous memory -

Just replace  
call print\_array(ival\_ptr)  
by:  
call print\_array(ival\_ptr(1:10:2))

> gfortran -fcheck=array-temps -Warray-temporaries main.F  
> main.F:13.23:  
> call print\_array(ival\_ptr(1:10:2))  
> 1  
> Warning: Creating array temporary at (1)

and a warning during execution:

> ./a.out  
> test::loc(ival)= 140733975879936  
> At line 13 of file main.F  
> Fortran runtime warning: An array temporary was created  
> print::loc(a)= 25096048

* * *

Analysis:

- version 1 :
  - print\_array receive the array ‘a’ as contiguous memory block
  - we see that both arraies have the same adress : loc(a) = loc(ival)
  - no runtime warning

- version 2 :
  - print-array receive a temporary array
  - loc(a) != loc(ival)
  - runtime warning

Questions :

- Version 2 I get the expected result, a warning during compilation and another at runtime
- Version 1 : I don’t understand the warning during compilation ?, is there a risk to pass a pointer as argument to a subroutine ?

What do you think ?

Thanks.

---

<div class="post-metadata">

**Author:** ![sblionel](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/sblionel/32/853_2.png) [@sblionel](https://fortran-lang.discourse.group/u/sblionel)\
**Post date:** [August 21, 2023, 4:11pm UTC](https://fortran-lang.discourse.group/t/fortran-pointer-as-argument-in-a-subroutine/6380/3 "2023-08-21T16:11:53Z")

</div>

In program 1, a run-time check is needed to see if a copy needs to be made, because a pointer can be discontiguous. gfortran evidently gives the compile time warning for this possibility, though perhaps the wording could be improved. Since you asked for warnings for temps, it’s fine that it did so in this case.

Intel Fortran doesn’t offer this compile-time warning (in any situation). It is fine to pass a pointer to a non-pointer dummy argument.

A side note: `LOC()` is an extension, but convenient to use in this test.

---

<div class="post-metadata">

**Author:** ![rfournier](https://avatars.discourse-cdn.com/v4/letter/r/eb8c5e/32.png) [@rfournier](https://fortran-lang.discourse.group/u/rfournier)\
**Post date:** [August 22, 2023, 6:09am UTC](https://fortran-lang.discourse.group/t/fortran-pointer-as-argument-in-a-subroutine/6380/4 "2023-08-22T06:09:16Z")

</div>

Thank you Steve.  
So I will use pointers safely 🙂

---

<div class="post-metadata">

**Author:** ![Beliavsky](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@Beliavsky](https://fortran-lang.discourse.group/u/Beliavsky)\
**Post date:** [August 22, 2023, 2:00pm UTC](https://fortran-lang.discourse.group/t/fortran-pointer-as-argument-in-a-subroutine/6380/5 "2023-08-22T14:00:28Z")

</div>

In the code you showed, it would be simpler to pass `ival` than `ival_ptr`. In your actual code, are you sure you need to pass a pointer?

---

<div class="post-metadata">

**Author:** ![rfournier](https://avatars.discourse-cdn.com/v4/letter/r/eb8c5e/32.png) [@rfournier](https://fortran-lang.discourse.group/u/rfournier)\
**Post date:** [August 22, 2023, 2:12pm UTC](https://fortran-lang.discourse.group/t/fortran-pointer-as-argument-in-a-subroutine/6380/6 "2023-08-22T14:12:03Z")

</div>

the code here is just a mockup to illustrate the issue. In the real code, I really need a fortran pointer because the memory is allocated in C. The link between c pointers and fortran pointers is done using C\_F\_POINTER !

---

<div class="post-metadata">

**Author:** ![Knarfnarf](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Knarfnarf](https://fortran-lang.discourse.group/u/Knarfnarf)\
**Post date:** [August 26, 2023, 2:51am UTC](https://fortran-lang.discourse.group/t/fortran-pointer-as-argument-in-a-subroutine/6380/7 "2023-08-26T02:51:29Z")

</div>

```auto
Subroutine this(variableA)
implicit none
! You are missing this next line !
integer, dimension(*), intent(inout) :: variableA 
print *, "This; ", variableA(1), "!"
end subroutine this

```

Have fun!

knarfnarF
