[Solved] POINTER keyword in FORTRAN equivalent in C/C++


According to the above page, the correspondence between Cray pointer and C pointer may be something like this (note, however, that Cray pointer is not the same as standardized pointer in modern Fortran).

Fortran:

integer    a( 3 ), i
integer*8  ptr
pointer( ptr, b(*) ); integer b

a(:) = 10
print *, "a = ", a( 1:3 )

ptr = loc( a(1) )    !! now b(i) becomes an alias of a(i) for i=1,2,3
b( 2 ) = 30

print *, "b = ", b( 1:3 )
print *, "a = ", a( 1:3 )

C:

int i, a[ 3 ] = { 10, 10, 10 };
int *ptr, *b;

printf( "a = %d %d %d\n", a[0], a[1], a[2] );

ptr = &( a[0] );   // <--> ptr = loc( a(1) ) in Fortran
b = ptr;           // <--> pointer( ptr, b(*) ) in Fortran

b[ 1 ] = 30;

printf( "b = %d %d %d\n", b[0], b[1], b[2] );
printf( "a = %d %d %d\n", a[0], a[1], a[2] );

Compile

gfortran -fcray-pointer test.f90
gcc test.c

a = 10 10 10
b = 10 30 10
a = 10 30 10

0

solved POINTER keyword in FORTRAN equivalent in C/C++