# Type of type : copy or reference?

**URL:** https://fortran-lang.discourse.group/t/type-of-type-copy-or-reference/8330
**Category:** Help
**Created:** [July 11, 2024, 3:01pm UTC](https://fortran-lang.discourse.group/t/type-of-type-copy-or-reference/8330 "2024-07-11T15:01:57Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Christophe](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/christophe/32/1906_2.png) [@Christophe](https://fortran-lang.discourse.group/u/Christophe)
#### Post date: [July 11, 2024, 3:01pm UTC](https://fortran-lang.discourse.group/t/type-of-type-copy-or-reference/8330/1 "2024-07-11T15:01:57Z")

</div>

Hello !

I have a type(toto) and the variable var0  
`type(toto) :: var0`

inside an other type(tata), I wish to have access to var0 so I introduce in this type the field

```auto
type tata
  ...
  type(toto) :: var0
  ...
end type

```

and when I create

`type(tata) :: var1`

I enter :

`var1%var0 = var0`

my question : is var1%var0 a **copy** of var0 or a **link** to var0 (i.e reference)

🤪

---

<div class="post-metadata">

### Author: ![themos](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/themos/32/521_2.png) [@themos](https://fortran-lang.discourse.group/u/themos)
#### Post date: [July 11, 2024, 3:26pm UTC](https://fortran-lang.discourse.group/t/type-of-type-copy-or-reference/8330/2 "2024-07-11T15:26:07Z")

</div>

Neither. “Copies” and “links” are not Fortran concepts.

 ![image](https://global.discourse-cdn.com/free1/uploads/fortran_lang/original/2X/a/ad962ce9de5ffd240cb130660967062d6b554d88.png)

---

<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: [July 11, 2024, 7:29pm UTC](https://fortran-lang.discourse.group/t/type-of-type-copy-or-reference/8330/3 "2024-07-11T19:29:13Z")

</div>

> [@Christophe](#):
>
> is var1%var0 a **copy** of var0 or a **link** to var0

neither. `var1` is a variable of type `tata`. `var1%var0` is a component that is of type `toto`, completely independent from the variable `var0`.

---

<div class="post-metadata">

### Author: ![Machalot](https://avatars.discourse-cdn.com/v4/letter/m/ebca7d/32.png) [@Machalot](https://fortran-lang.discourse.group/u/Machalot)
#### Post date: [July 12, 2024, 1:36am UTC](https://fortran-lang.discourse.group/t/type-of-type-copy-or-reference/8330/4 "2024-07-12T01:36:57Z")

</div>

If you wish to have access to `var0`, the original, by reference from within the context of the a derived type variable `var1` (which of of derived type `tata`), you can use a pointer. (I’m going out on a limb here because I don’t often use pointers, so I’m going by my understanding of how it should work.)

Declare `var0` as a target using `type(toto), target :: var0`.

Define a member field within `tata` that is a pointer to a variable of type `toto`, such as `type(toto), pointer :: any_name` (it doesn’t have to be called `var0`).

Then associate the pointer using `var1%any_name => var0`.

If you’d rather have a copy of `var0` then delete `target` and `pointer` from the above, and use `=` instead of `=>`.

Edit: This might be a clearer example.

```fortran
module mymod

type a
  real :: y = 3.14
end type a

type b
  type(a), pointer :: g
end type b

type c
  type(a) :: h
end type c

end module mymod

use mymod

type(a), target :: x
type(b) :: w
type(c) :: z

w%g => x
z%h = x

print *, 'Initial value'
print *, x ! original variable
print *, w%g ! reference to original variable
print *, z%h ! copy of original variable

x%y = 6.28

print *
print *, 'Updated value'
print *, x ! original variable
print *, w%g ! reference to original variable
print *, z%h ! copy of original variable

end

```

Results with `gfortran`:

```bash
 Initial value
   3.14000010
   3.14000010
   3.14000010

 Updated value
   6.28000021
   6.28000021
   3.14000010

```

---

<div class="post-metadata">

### Author: ![Christophe](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/christophe/32/1906_2.png) [@Christophe](https://fortran-lang.discourse.group/u/Christophe)
#### Post date: [July 12, 2024, 7:13am UTC](https://fortran-lang.discourse.group/t/type-of-type-copy-or-reference/8330/5 "2024-07-12T07:13:23Z")

</div>

Thanks I am going to test that solution.

I asked that question because I am facing strange behavior with intel compilers but every thing is fine with gnu compilers (usually intel are more flexible that gnu compilers)

Attribute target seems to transform a variable as a pointer (without dimension).

Between contiguous, protected, target… I am a little bit confused with these attributes and I usually avoid using it in order to keep more simplicity in codes.

---

<div class="post-metadata">

### Author: ![themos](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/themos/32/521_2.png) [@themos](https://fortran-lang.discourse.group/u/themos)
#### Post date: [July 12, 2024, 10:09am UTC](https://fortran-lang.discourse.group/t/type-of-type-copy-or-reference/8330/6 "2024-07-12T10:09:12Z")

</div>

```auto
program test
  type :: t1
     real :: np
     real,pointer :: p => null()
  end type t1
  type(t1), target :: x,y

  x=t1(10.0)
  x%p => x%np
  Print *,x%np, x%p

  y = x
  Print *,y%np, y%p

  x%np=5.0
  Print *,y%np, y%p

end program

```

As you can verify, the intrinsic assignment `y=x` produces the effect of intrinsic assignment for nonpointer components (informally, ‘copy’) and pointer assignment for pointer components (informally, ‘reference’).
