For Programmers: Free Programming Magazines  


Home > Archive > Fortran > May 2004 > How to get address of an array









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author How to get address of an array
Shuo Yang

2004-04-27, 9:35 pm

I have a two dimension array in fortran,
double precision buffer(size, 4)

I want to get the 2nd row's starting address, i.e, buffer(1, 2). How can
I do it?
Is there a way similar to C like
//what I want to do here
{
char* p;
p= &(buffer[1,2])
}
Thanks.

Dr Ivan D. Reid

2004-04-28, 4:37 am

On Tue, 27 Apr 2004 19:04:44 -0500, Shuo Yang <yang22@purdue.edu>
wrote in <408EF51C.9090504@purdue.edu>:
> I have a two dimension array in fortran,
> double precision buffer(size, 4)


> I want to get the 2nd row's starting address, i.e, buffer(1, 2). How can
> I do it?


For what purpose? Intent can make a difference.

> Is there a way similar to C like
> //what I want to do here
> {
> char* p;
> p= &(buffer[1,2])
> }


Most Fortrans pass "as if" by reference, so if you want to pass the
second row to a subroutine "call sub(buffer(1,2))" will do what you (perhaps)
want.

--
Ivan Reid, Electronic & Computer Engineering, ___ CMS Collaboration,
Brunel University. Ivan.Reid@brunel.ac.uk Room 40-1-B12, CERN
KotPT -- "for stupidity above and beyond the call of duty".
Les

2004-04-28, 5:54 am


"Shuo Yang" <yang22@purdue.edu> wrote in message
news:408EF51C.9090504@purdue.edu...
> I have a two dimension array in fortran,
> double precision buffer(size, 4)
>
> I want to get the 2nd row's starting address, i.e, buffer(1, 2). How can
> I do it?
> Is there a way similar to C like
> //what I want to do here
> {
> char* p;
> p= &(buffer[1,2])
> }
> Thanks.
>


If you have the LOC function available.

"LOC
Inquiry Intrinsic Function (Generic): Returns the internal address of a
storage item. This function cannot be passed as an actual argument.

Syntax

result = LOC (x)

x
(Input) Is a variable, an array or record field reference, a procedure, or
a constant; it can be of any data type. It must not be the name of an
internal procedure or statement function. If it is a pointer, it must be
defined and associated with a target. "


Dave Seaman

2004-04-28, 10:48 am

On Tue, 27 Apr 2004 19:04:44 -0500, Shuo Yang wrote:
> I have a two dimension array in fortran,
> double precision buffer(size, 4)


> I want to get the 2nd row's starting address, i.e, buffer(1, 2). How can
> I do it?


Fortran stores its matrices by columns, unlike C. A row of a matrix
consists of nonconsecutive memory locations, unless the number of rows is
1. In Fortran 90 notation, the second row is buffer(2,:), while the
second column is buffer(:,2).

> Is there a way similar to C like
> //what I want to do here
> {
> char* p;
> p= &(buffer[1,2])
> }
> Thanks.


What are you actually trying to do? You haven't done anything with p
here.

For example, if you want to add 1 to each member of the second row in
Fortran 90, you can write

buffer(2,:) = buffer(2,:) + 1

Fortran 90 also has pointers, but they are quite different from C
pointers. You should try to get used to the array notation first.


--
Dave Seaman
Judge Yohn's mistakes revealed in Mumia Abu-Jamal ruling.
<http://www.commoncouragepress.com/i...book&bookid=228>
Shuo Yang

2004-05-04, 3:05 pm



Dr Ivan D. Reid wrote:
> On Tue, 27 Apr 2004 19:04:44 -0500, Shuo Yang <yang22@purdue.edu>
> wrote in <408EF51C.9090504@purdue.edu>:
>
>
>
>
>
>
> For what purpose? Intent can make a difference.

~~~~~~~~~~~~~~~~~I will use it as a parameter passing to a
subroutine. call sendbuffer( pointer, size_to_send). It's very
convenient to have pointer aligned to some address of a matrix in my
application

>
>
>
>
> Most Fortrans pass "as if" by reference, so if you want to pass the
> second row to a subroutine "call sub(buffer(1,2))" will do what you (perhaps)
> want.
>


Shuo Yang

2004-05-04, 3:05 pm



Dave Seaman wrote:
> On Tue, 27 Apr 2004 19:04:44 -0500, Shuo Yang wrote:
>
>
>
>
>
> Fortran stores its matrices by columns, unlike C. A row of a matrix
> consists of nonconsecutive memory locations, unless the number of rows is
> 1. In Fortran 90 notation, the second row is buffer(2,:), while the
> second column is buffer(:,2).
>
>
>
>
> What are you actually trying to do? You haven't done anything with p
> here.

~~~~~~~~~~~~after above operation, I would do
sendbuffer(p, size_to_send);

>
> For example, if you want to add 1 to each member of the second row in
> Fortran 90, you can write
>
> buffer(2,:) = buffer(2,:) + 1
>
> Fortran 90 also has pointers, but they are quite different from C
> pointers. You should try to get used to the array notation first.
>
>


Dr Ivan D. Reid

2004-05-04, 3:05 pm

On Mon, 03 May 2004 23:06:25 -0500, Shuo Yang <yang22@purdue.edu>
wrote in <409716C1.5020501@purdue.edu>:

> Dr Ivan D. Reid wrote:
[color=darkred]
[color=darkred]
[color=darkred]
> ~~~~~~~~~~~~~~~~~I will use it as a parameter passing to a
> subroutine. call sendbuffer( pointer, size_to_send). It's very
> convenient to have pointer aligned to some address of a matrix in my
> application


...and unnecessary in Fortran (usually). Use
CALL SENDBUFFER(buffer(1,2),size_to_send)

--
Ivan Reid, Electronic & Computer Engineering, ___ CMS Collaboration,
Brunel University. Ivan.Reid@brunel.ac.uk Room 40-1-B12, CERN
KotPT -- "for stupidity above and beyond the call of duty".
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com