change "pointer to pointer" in C to something else in Fortran 
Author Message
 change "pointer to pointer" in C to something else in Fortran

Hi,

I am not familar with C, there is a subroutine written in C need to be
changed into fortran. The definition of the C function is something like:

int SetAA(void **p) {...}

It is called by some other C routines. I need to change the above subroutine
into a Fortran Code without any modifitions to other routines. As there is
no pointer-to-pointer argument in Fortran and I can not modify anything in
other routines that call this sub. I am wondering if this can be done and is
there anyone who can give me a hand?

Regards

Wusheng



Sun, 31 Jul 2005 20:50:58 GMT  
 change "pointer to pointer" in C to something else in Fortran

| Hi,
|
| I am not familar with C, there is a subroutine written in C need to be
| changed into Fortran. The definition of the C function is something like:
|
| int SetAA(void **p) {...}
|
| It is called by some other C routines. I need to change the above subroutine
| into a Fortran Code without any modifitions to other routines. As there is
| no pointer-to-pointer argument in Fortran and I can not modify anything in
| other routines that call this sub. I am wondering if this can be done and is
| there anyone who can give me a hand?

We need more info about the context; particularly, HOW is p used in SetAA?
In C, void** is usually passed for one of the following reasons:

a) It needs to have a piece of memory allocated within the routine & returned
   a pointer to it;
b) It is used as a (C emulation of) multi-dimensional array.

Post some relevant parts of SetAA code so maybe someone can come up with a
solution.

--
 Jugoslav
___________
www.geocities.com/jdujic



Sun, 31 Jul 2005 21:18:02 GMT  
 change "pointer to pointer" in C to something else in Fortran


Quote:
> Hi,

> I am not familar with C, there is a subroutine written in C need to
be
> changed into Fortran. The definition of the C function is something
like:

> int SetAA(void **p) {...}

Someone has a current topic in comp.lang.c
 " Passing void** to function for assignment portable? "

the consensus seems to be that its not portable within the C world,
so how do you expect to translate it to fortran without a complete
understanding what it accomplishes for the originating C
programmer?



Sun, 31 Jul 2005 22:26:34 GMT  
 change "pointer to pointer" in C to something else in Fortran
Thank you very much for the reply, I post some code details as follows,

The code:
==================================
#include <......>

FPTR_VOID_CHAR  g_fPtrA1= NULL;
FPTR_VOID_INT      g_fPtrA2= NULL;
FPTR_VOID_INT      g_fPtrA3= NULL;

int SetAA(int iNum, void **p)
{
 int iC = 0;

 g_fPtrA1 = (FPTR_VOID_CHAR)p[iC++];
 if(iC + 1 > iNum)
  return iC;

 g_fPtrA2    = (FPTR_VOID_INT)p[iC++];
 if(iC + 1 > iNum)
  return iC;

 g_fPtrA3 = (FPTR_VOID_INT)p[iC++];
 if(iC + 1 > iNum)
  return iC;
.
.
.

Quote:
}

I reckon this is something you mentioned that the pointer-to-pointer is
used as a (C emulation of) multi-dimensional array. I am doubting about
whether using the pointer array in Fortran can implement the function, the
main concern is eventually it has to be called by some other C routines.
 Is there anyone have the experience on this and can show me how to do it.

Meanwhile, a further question is that how the type cast function in
C can be implemented in Fortran. May be I should post this separately.

Many thanks

Wusheng


Quote:


> | Hi,
> |
> | I am not familar with C, there is a subroutine written in C need to be
> | changed into Fortran. The definition of the C function is something
like:
> |
> | int SetAA(void **p) {...}
> |
> | It is called by some other C routines. I need to change the above
subroutine
> | into a Fortran Code without any modifitions to other routines. As there
is
> | no pointer-to-pointer argument in Fortran and I can not modify anything
in
> | other routines that call this sub. I am wondering if this can be done
and is
> | there anyone who can give me a hand?

> We need more info about the context; particularly, HOW is p used in SetAA?
> In C, void** is usually passed for one of the following reasons:

> a) It needs to have a piece of memory allocated within the routine &
returned
>    a pointer to it;
> b) It is used as a (C emulation of) multi-dimensional array.

> Post some relevant parts of SetAA code so maybe someone can come up with a
> solution.

> --
>  Jugoslav
> ___________
> www.geocities.com/jdujic



Mon, 01 Aug 2005 00:36:42 GMT  
 change "pointer to pointer" in C to something else in Fortran
Below inquiry was made before you partially replied with function
statements (but no caller side statements)..



Quote:


> > Today in comp.lang.fortran someone asked how to translate a
function
> > call below to Fortran.

> > int SetAA(void **p) {  }

> > As a non-C person interested in C -> Fortran translations,
> > I would like to see a VERY simple program that calls such a
function
> > with a minimum of statements in the function and then the exec
outputs
> > something to give a test result (shown).
> > Can you or someone post such a minimal (c)ritter?

> #include <stdio.h>
> #include <stdlib.h>

> int SetAA( void **p )
> {
>   int i;

>   *p = malloc( 5 * sizeof(int) );
>   for( i = 0; i < 5; ++i )
>     ((int*)*p)[i] = i;

>   return 5;
> }

> int main()
> {
>   void* pTest;
>   int   i, Nr;

>   Nr = SetAA( &pTest );

>   for( i = 0; i < 5; ++i )
>     printf( "%d\n", ((int*)pTest)[i] );

>   return 0;
> }

> --
> Karl Heinz Buchegger


Karl, FWIW below is my translation of above to Fortran

! --------------
module test_1
contains
function SetAA(p)  result (Nr)
implicit none
integer,allocatable :: p(:)
integer :: Nr, i
allocate ( p(0:4) )
p = [0,1,2,3,4]
Nr = 5
end function
end module
! --------------
program test
use test_1     ! contains function SetAA
implicit none
integer,allocatable :: a(:)
integer :: n
n = SetAA(a)
write (*,*) '#allocated=',size(a)    ! outputs 5
write (*,*) '#values=', a            ! outputs 0 1 2 3 4
end program



Mon, 01 Aug 2005 02:46:42 GMT  
 change "pointer to pointer" in C to something else in Fortran

| Thank you very much for the reply, I post some code details as follows,
|
|
| The code:
| ==================================
| #include <......>
|
|
| FPTR_VOID_CHAR  g_fPtrA1= NULL;
| FPTR_VOID_INT      g_fPtrA2= NULL;
| FPTR_VOID_INT      g_fPtrA3= NULL;
|
|
| int SetAA(int iNum, void **p)
| {
|  int iC = 0;
|
|  g_fPtrA1 = (FPTR_VOID_CHAR)p[iC++];
|  if(iC + 1 > iNum)
|   return iC;
|
|  g_fPtrA2    = (FPTR_VOID_INT)p[iC++];
|  if(iC + 1 > iNum)
|   return iC;
|
|  g_fPtrA3 = (FPTR_VOID_INT)p[iC++];
|  if(iC + 1 > iNum)
|   return iC;

Uh-oh, so p is used as address of some kind of lookup table to
various kinds of data. It's hard to mimic that in Fortran -- not
impossible, but hard and (probably) not portable.

That code (ab)uses the ability of C to manipulate with raw addresses
freely, and to point with apples to oranges. One of main features
of Fortran -- which enables some of its optimization strengths --
is that you can't point with apples to oranges; Fortran is far more
type safe than C. So, re your other question, you can't do "raw"
typecasts in standard Fortran freely (i.e. interpret constants of
an arbitrary memory location once as int, the other time as char).

But before I continue, I must say that your main problem will not
be how to mimic void**, but scope of g_fPtrA1 etc. -- usage and
name suggests that these are used as *global variables*. If setAA were some
kind of "black box", pure translation probably could be done somehow,
but the *real* question is -- HOW ARE YOU GOING TO ACCESS g_f VARIABLES
if you're not allowed to change the rest of C program?

There are means to mimic functionality of that C code in Fortran, namely
one or more of:
- Playing tricks with Fortran POINTERs
- Using TRANSFER intrinsic (TRANSFER does typecast, but by means of
  copying, not by reinterpreting)
- Using EQUIVALENCE (probably useless in your case, since it requres
  static storage)
- Using non-standard "Cray" pointers (whose semantic is very similar
  to C pointers, and are reasonably widespread extension).

But I wouldn't elaborate at this moment; even if it is doable, you'll
end up with Fortran code that smells like C. So, my suggestion is that
you give up and continue to use C for that. If you *really* can't, please

- Let us know what your OS and Fortran compiler are.
- Are g_fPtrA1 etc. used throughout the rest of C code and how?
- Why do you *must* translate that to Fortran?

Sorry for not being more helpful, but your problem is still vague,
and IMO it's better to present you the problems and alternatives than
to try to respond to the question blindly.

--
 Jugoslav
___________
www.geocities.com/jdujic



Mon, 01 Aug 2005 16:45:54 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Use of pointer "association" in Fortran

2. pointer-delimited strings, "$egments", in H3sm 1.4

3. Eiffel "Gocha" #5 - null pointers

4. Lets define "pointer"

5. Lets define "pointer"

6. Passing a function "pointer"

7. COBOL FAQ - frequency of "pointer" posting

8. "C" style Function pointers

9. Array "pointers" in fortran77

10. "Attn" COBOL FAQ (pointer to URL)

11. Pointers within "f77" under linux

12. Pointers within "f77" under linux

 

 
Powered by phpBB® Forum Software