Interop boundaries often pass a pointer plus explicit shape metadata. This example models the metadata while keeping the data in Fortran.

Program

Play the program to choose how many values are considered active.

shape_metadata.f90
program shape_metadata_demo
    use iso_c_binding, only: c_int
    implicit none
    integer(c_int) :: values(4)
    integer(c_int) :: active_count
    integer(c_int) :: total

    values = [2_c_int, 4_c_int, 6_c_int, 8_c_int]
    active_count = 
    total = sum(values(1:active_count))
    print '(I0, 1X, I0)', active_count, total
end program shape_metadata_demo
program shape_metadata_demo
    use iso_c_binding, only: c_int
    implicit none
    integer(c_int) :: values(4)
    integer(c_int) :: active_count
    integer(c_int) :: total

    values = [2_c_int, 4_c_int, 6_c_int, 8_c_int]
    active_count = 
    total = sum(values(1:active_count))
    print '(I0, 1X, I0)', active_count, total
end program shape_metadata_demo
program shape_metadata_demo
    use iso_c_binding, only: c_int
    implicit none
    integer(c_int) :: values(4)
    integer(c_int) :: active_count
    integer(c_int) :: total

    values = [2_c_int, 4_c_int, 6_c_int, 8_c_int]
    active_count = 
    total = sum(values(1:active_count))
    print '(I0, 1X, I0)', active_count, total
end program shape_metadata_demo
explicit count `active_count` models shape metadata that accompanies array data.
section `values(1:active_count)` selects only the active portion.
C kind array `integer(c_int)` documents the intended interoperable element type.