From: Richard Maine on
bio_amateur <hoangtrongminhtuan(a)gmail.com> wrote:

> Now, the situation is that I have an array which is quite large, and
> passing by reference to the C function. Now, we'll see that creating a
> duplicate is not a good choice. So, if we know a way, such as using
> pointer to typecast and pass it to the C function. We can get higher
> performance.

A little unusual to have a large array of type c_size_t, but perhaps
that was a different question (as you example of that was not an array
at all, much less a large one, and was passed by value). I'm left
slightly confused as to whether this is a correction/elaboration of the
original problem or is a separate one.

One does not "use a pointer to typecast". That is mixing up several
concepts.

One can, however, get a c_ptr to any appropriately interoperable
variable. See the C_LOC function. Having a C pointer, you can then pass
that to C and "abuse" it in whatever way you can manage in C. Whether it
will achieve what you want is up to you to figure out. I can't tell from
your description, but in any case that is more a C question than a
Fortran one (and it seems likely to be system/compiler dependent).

One can also take a c_ptr, possibly one obtained from C_LOC, and create
a Fortran pointer from it (see the c_f_pointer function). You are
"supposed" to only to that if the Fortran pointer is the "right" type
for the data being pointed to. But compilers are unlikely to be able to
check that (I did only say "unlikely", which gives me an out if you find
one that manages to do such a check in simple cases). This probably
gives you a way to "lie" to the compiler and do much of the same kind of
trickery that you can often get by with in C. As with most such
trickery, it is again up to you to figure out if it will achieve the
intended result. If you expect it to achieve "magic", you are likely to
be dissappointed. Changing the physical representation without either
copying or altering the original would count as magic.

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