From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 26062 invoked by alias); 27 Mar 2010 20:54:31 -0000 Received: (qmail 26052 invoked by uid 22791); 27 Mar 2010 20:54:30 -0000 X-SWARE-Spam-Status: No, hits=-1.5 required=5.0 tests=AWL,BAYES_00 X-Spam-Check-By: sourceware.org Received: from mail-gw0-f47.google.com (HELO mail-gw0-f47.google.com) (74.125.83.47) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Sat, 27 Mar 2010 20:54:25 +0000 Received: by gwaa20 with SMTP id a20so2406635gwa.20 for ; Sat, 27 Mar 2010 13:54:23 -0700 (PDT) Received: by 10.151.88.2 with SMTP id q2mr3061168ybl.75.1269723263834; Sat, 27 Mar 2010 13:54:23 -0700 (PDT) Received: from [192.168.1.6] (cpe-76-168-78-146.socal.res.rr.com [76.168.78.146]) by mx.google.com with ESMTPS id 23sm2167626iwn.14.2010.03.27.13.54.22 (version=TLSv1/SSLv3 cipher=RC4-MD5); Sat, 27 Mar 2010 13:54:23 -0700 (PDT) Message-ID: <4BAE7294.9070608@gmail.com> Date: Sun, 28 Mar 2010 20:22:00 -0000 From: burlen User-Agent: Thunderbird 2.0.0.23 (X11/20090817) MIME-Version: 1.0 To: Ian Lance Taylor CC: gcc-help@gcc.gnu.org Subject: Re: fortran 90 passing user defined type member to a c fucntion References: <4BAD0026.5010909@gmail.com> <4BAE42FF.7020200@gmail.com> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-IsSubscribed: yes Mailing-List: contact gcc-help-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-help-owner@gcc.gnu.org X-SW-Source: 2010-03/txt/msg00335.txt.bz2 Ian Lance Taylor wrote: > burlen writes: > > >> Ian Lance Taylor wrote: >> >>> burlen writes: >>> >>> >>> >>>> When calling a c function from a fortran 90 program with members of a >>>> user defined types for output arguments of the called subroutine the >>>> user defined types aren't being modified as they should be (according >>>> to my understanding). >>>> >>>> >>> When you call a C function, you get the C rules. It doesn't matter >>> whether you are calling it from Fortran or not. In C, a modification >>> of an argument is not reflected back to the caller. >>> >>> Ian >>> >>> >> In my understanding, Fortran unless otherwise instructed is supposed >> to pass by address. In the c function modifying the data pointed to >> should do just that, and be visible to the caller. What I don't >> understand is why passing the member of a user defined type behaves as >> if it's passed by value in this case, while passing the corresponding >> native type is passed by address and works as expected. >> > > > I think you are going to have to provide an example. > > > If you write a C function > > void foo(int i) { i = 1; } > > then the parameter is not going to change in the caller. That's just > how C works. There is no reasonable way to change that. > > Ian > True. But when you're mixing fortran and c you don't write your c function that way, because fortran passes by address. I don't know if it's mandated by a standard or not, but that's just how it's implemented in my experience. I have used mixed language programming with gnu, intel, and portland group. This is my first attempt at making use of the so called object oriented features of fortran 90 . For example your function would have to be written as follows: /* start of c code */ void foo_(int *i) { (*i)=1; } /* end of c code */ This can be called from a fortran program as follows: ! start of fortran code program main integer i i=0 call foo(i) write(0,*)i end program ! end of fortran code compile like this: $ gcc ccode.c -c -o ccode.o $ gfortran fcode.f90 ccode.o -o fcode $ ./fcode 1