From: M.S. Breitenfeld on
Is it valid to use C_LOC on a component of a derive type? example:

PROGRAM main

USE ISO_C_BINDING
IMPLICIT NONE

TYPE, BIND(C) :: comp_datatype
INTEGER(C_INT) :: x
END TYPE comp_datatype

TYPE(comp_datatype), TARGET :: fill_ctype
TYPE(C_PTR) :: f_ptr

f_ptr = C_LOC(fill_ctype%x)

END

I get the error from one of the compilers I'm using:

ERROR: C_LOC function argument must be a variable that has the TARGET or
POINTER attribute, or an allocated allocatable variable that has the
TARGET attribute.
From: Richard Maine on
M.S. Breitenfeld <msbrtnfld(a)gmail.com> wrote:

> Is it valid to use C_LOC on a component of a derive type?

Yes.

> PROGRAM main
>
> USE ISO_C_BINDING
> IMPLICIT NONE
>
> TYPE, BIND(C) :: comp_datatype
> INTEGER(C_INT) :: x
> END TYPE comp_datatype
>
> TYPE(comp_datatype), TARGET :: fill_ctype
> TYPE(C_PTR) :: f_ptr
>
> f_ptr = C_LOC(fill_ctype%x)
>
> END
>
> I get the error from one of the compilers I'm using:
>
> ERROR: C_LOC function argument must be a variable that has the TARGET or
> POINTER attribute, or an allocated allocatable variable that has the
> TARGET attribute.

Well, the error message alone tells you that something is wrong with the
compiler, because it tells you what the compiler is complaining about,
and that complaint is wrong. In particular, fill_ctype%x is a variable
(a subobject of a variable is a variable) and it does have the target
attribute (a subobject of a variable with the target attribute also has
the target attribute).

Thus even if there were some restriction against using C_LOC on a
component of a derived type (there isn't), that's not what the error
message says and thus the compiler wuld still be in the wrong for giving
the wrong error message.

Also, something else is a bit odd about the error message in that it is
redundant. An allocated allocatable variable is a variable. There are
places where Fortran terminology defies the normal rules of English
usage in that an adjectives don't necessarily modify nouns in the normal
English sense. In English an "adjective noun" is generally a specific
case of "noun". For example, a blue ball is a ball. But in Fortran
standard-speak, we have things like an executable construct, which is
not necessarily, or even usually, a construct. However, an allocated
allocatable variable is not one of those oddities; it is indeed a
particular kind of variable, as normal English usage would lead you to
believe. Thus the "or an allocated allocatable variable" part of that
error message is redundant and misleading at best.

--
Richard Maine | Good judgment comes from experience;
email: last name at domain . net | experience comes from bad judgment.
domain: summertriangle | -- Mark Twain