https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61788

            Bug ID: 61788
           Summary: use of x[nr][nc] as array parameter passing mechanism
                    in mixed language programming
           Product: gcc
           Version: 4.8.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ian at rhymneyconsulting dot co.uk

I am writing a suite of mixed language program examples (Fortran calling C, C
calling Fortran, and C++ calling Fortran) and have hit a problem with the
following code. I am not sure if it is a bug or a standards conformance issue.

With the following code C++ 

#####

#include <iostream>

using namespace std;

extern "C" void reciprocal(int nr,int nc,float x[nr][nc],float y[nr][nc]);

int main()
{
  const int nr=2;
  const int nc=5;
  float x[nr][nc];
  float y[nr][nc];
  float t[]={1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0};
  int r;
  int c;
  int i=0;

  for (r=0;r<nr;r++)
    for (c=0;c<nc;c++)
    {
      x[r][c]=t[i];
      i++;
    }

  cout << " C++ passing a 2d array to Fortran " << endl;

  for (r=0;r<nr;r++)
  {
    for (c=0;c<nc;c++)
    {
      cout << x[r][c] << " ";
    }
    cout << endl;
  }

  reciprocal(nr,nc,x,y);

  for (r=0;r<nr;r++)
  {
    for (c=0;c<nc;c++)
      cout << " 1 / " << x[r][c] << " = " << y[r][c] << endl;
  }

  return(0);
}

#####

and the following Fortran code

#####

subroutine reciprocal(nr,nc,x,y) bind(c,name='reciprocal')
use iso_c_binding
implicit none
integer (c_int) , value :: nr
integer (c_int) , value :: nc
real (c_float) , dimension(1:nr,1:nc) , intent(in ) :: x
real (c_float) , dimension(1:nr,1:nc) , intent(out) :: y 
  y=1.0/x
end subroutine reciprocal

#####

I get the following error message.

#####

$ g++ ch3110.cxx ch3110_f.o
ch3110.cxx:5:50: error: use of parameter ‘nr’ outside function body
 extern "C" void reciprocal(int nr,int nc,float x[nr][nc],float y[nr][nc]);
                                                  ^
ch3110.cxx:5:54: error: use of parameter ‘nc’ outside function body
 extern "C" void reciprocal(int nr,int nc,float x[nr][nc],float y[nr][nc]);
                                                      ^
ch3110.cxx:5:57: error: expected ‘)’ before ‘,’ token
 extern "C" void reciprocal(int nr,int nc,float x[nr][nc],float y[nr][nc]);
                                                         ^
ch3110.cxx:5:58: error: expected unqualified-id before ‘float’
 extern "C" void reciprocal(int nr,int nc,float x[nr][nc],float y[nr][nc]);

#####


The C calling routine works.

#####

#include <stdio.h>

void reciprocal(int nr,int nc,float x[nr][nc],float y[nr][nc]);

int main()
{
  const int nr=2;
  const int nc=5;
  float x[nr][nc];
  float y[nr][nc];
  float t[]={1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0};
  int r;
  int c;
  int i=0;

  for (r=0;r<nr;r++)
    for (c=0;c<nc;c++)
    {
      x[r][c]=t[i];
      i++;
    }

  printf(" C passing a 2d array to Fortran\n");

  for (r=0;r<nr;r++)
  {
    for (c=0;c<nc;c++)
    {
      printf(" %f " , x[r][c]);
    }
    printf("\n");
  }

  reciprocal(nr,nc,x,y);

  for (r=0;r<nr;r++)
  {
    for (c=0;c<nc;c++)
    {
      printf(" 1 / %f = %f \n " , x[r][c],y[r][c]);
    }
    printf("\n");
  }

  return(0);
}

#####

Any thoughts?

Reply via email to