0

I am having difficulty passing a 2d array from Fortran to C function. However, after all the support the following code is functional 100%.

The following is my C function:

#include <stdio.h>
  void print2(void *p, int n)
  {
     printf("Array from C is \n");
     double *dptr;
     dptr = (double *)p;
     for (int i = 0; i < n; i++)
     {
        for (int j = 0; j<n; j++)
            printf("%.6g \t",dptr[i*n+j]);

        printf("\n");
     }
  }

The following is my Fortran code:

        program linkFwithC
        use iso_c_binding
        implicit none 
        interface
          subroutine my_routine(p,r) bind(c,name='print2')
            import :: c_ptr
            import :: c_int
            type(c_ptr), value :: p
            integer(c_int), value :: r
          end subroutine
        end interface


        integer,parameter ::n=3
        real (c_double), allocatable, target :: xyz(:,:)
        real (c_double), target :: abc(3,3)
        type(c_ptr) :: cptr
        allocate(xyz(n,n))
        cptr = c_loc(xyz(1,1))

        !Inputing array valyes

        xyz(1,1)= 1
        xyz(1,2)= 2
        xyz(1,3)= 3
        xyz(2,1)= 4
        xyz(2,2)= 5
        xyz(2,3)= 6
        xyz(3,1)= 7
        xyz(3,2)= 8
        xyz(3,3)= 9


        call my_routine(cptr,n)
        deallocate(xyz)
      pause
      end program linkFwithC

The code runs fine;however, the array elements in C need to be re-organized.

Note, In order to link the C function with the FORTRAN code in a visual studio environment, one should follow the following step:

  • Write the C function in static library project
  • build the .lib file
  • Create the FORTRAN project and write your code
  • Add the .lib file to the FORTRAN project(just add it to the sources file)
  • Compile and run.

Thanks, Anas

Anas
  • 359
  • 1
  • 5
  • 14
  • I have noticed with `C#` (so possibly with other languages also) that the byte order of a `double` is revered when passed to `FORTRAN`. If your code works fine for `float` but not for `double` then this is what is going on. – John Alexiou Dec 20 '14 at 22:16
  • Neither float nor double worked. Is there a specific way of passing a two dimensional array to C? – Anas Dec 20 '14 at 22:20
  • Okay, after a throughout research I was able to fix the problem by using the allocate command to create the array and then assign the address of the first element of the array to the pointer ptr. This pointer (In Fortran) is then passed to the C function. Please see the code in the question for further details. – Anas Dec 22 '14 at 00:35

1 Answers1

-1

There are two ways arrays like you are passing are stored - ROW-MAJOR and COLUMN-MAJOR. C uses row-major, fortran uses column-major.

You will have to access the array elements in C in the order as it was created in fortran.

Rather than post a code block, you might do better to understand it before you code it. See this for clarity: http://en.wikipedia.org/wiki/Row-major_order

jim mcnamara
  • 16,005
  • 2
  • 34
  • 51
  • 1
    Although the question is unclear about "all wrong", I think that it's unlikely that this is the problem observed: please see the answer in the linked (duplicate) question given by IanH. – francescalus Dec 21 '14 at 00:27
  • I have found a way to pass the 2d array by just passing a pointer to the first element of the array. Please see the code in the question – Anas Dec 22 '14 at 00:45