# Pointer to protected variable

**URL:** <https://fortran-lang.discourse.group/t/pointer-to-protected-variable/9560>\
**Category:** Uncategorized\
**Created:** [April 7, 2025, 8:40pm UTC](https://fortran-lang.discourse.group/t/pointer-to-protected-variable/9560 "2025-04-07T20:40:46Z")\
**Posts on this page:** 4\
**Page:** 1

<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:** [April 7, 2025, 8:40pm UTC](https://fortran-lang.discourse.group/t/pointer-to-protected-variable/9560/1 "2025-04-07T20:40:46Z")

</div>

Consider the following snippet:

```fortran
module string_mod
implicit none
character(len=8), protected, target :: str = 'abcdefgh'
contains
    function ptr_to_str()
        character(len=:), pointer :: ptr_to_str
        ptr_to_str => str
    end function
end module

program test
    use string_mod
    implicit none
    character(len=:), pointer :: p
    p => ptr_to_str()
    print *, p
    p = repeat('a',len(p))
    print *, p
end program

```

Is it allowed to modify the protected variable via the pointer returned by the procedure? The constraints in 8.5.15 (J3/24-007) only talk about use association.

Paragraph 2 of 8.5.15 says:

> Other than within the module in which an entity is given the PROTECTED attribute, or within any of its descendants,
> 
> - if it is a nonpointer object, **it is not definable** , and

Since `str` is not a pointer object, I suspect what I’m doing here is “illegal”.

Related questions:

- [What is PROTECTED supposed to protect for a pointer?](https://fortran-lang.discourse.group/t/what-is-protected-supposed-to-protect-for-a-pointer/4759) - As explained here a `protected, pointer` variable can still be modified, the protection only applies to the pointer association status, not the target. I believe the example above is slightly different.

Output of a few compilers:

```txt
> nagfor test_protected.f90 
NAG Fortran Compiler Release 7.2(Shin-Urayasu) Build 7203
[NAG Fortran Compiler normal termination]
> ./a.out
 abcdefgh
 aaaaaaaa

```

```auto
> gfortran test_protected.f90 
> ./a.out
 abcdefgh
 aaaaaaaa

```

---

<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:** [April 7, 2025, 9:35pm UTC](https://fortran-lang.discourse.group/t/pointer-to-protected-variable/9560/2 "2025-04-07T21:35:29Z")

</div>

The code is valid. As you found, the `protected` attribute prohibits changing the definition status of the pointer (from outside the module in this case). There is not currently a feature that blocks redefinition of the target. There was a proposal to do this for F2023 but it did not get completed; it’s not officially on the work list for F202Y.

---

<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:** [April 7, 2025, 9:41pm UTC](https://fortran-lang.discourse.group/t/pointer-to-protected-variable/9560/3 "2025-04-07T21:41:54Z")

</div>

So that means `protected` only applies through use association? Seems kind of strange it is so easy to “defeat” the protection.

---

<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:** [April 7, 2025, 11:06pm UTC](https://fortran-lang.discourse.group/t/pointer-to-protected-variable/9560/4 "2025-04-07T23:06:21Z")

</div>

> [@ivanpribec](#):
>
> Seems kind of strange it is so easy to “defeat” the protection.

I think the idea is that the programmer has control over access to the variable from rather strict to moderate to almost open. Making it a public target is the “almost open” end of the spectrum. Making a setter function (with public or private attribute) is in the moderate range, and making it only private to the module with no outside access at all is in the strict range. However, even in this latter case, there are some workarounds that can allow the variable to be modified outside of the module (e.g. through a subroutine argument list or through local pointer=\>target associations). I’m unsure how much the compiler is expected to do to support the programmer’s intentions in some of these cases.
