comp.lang.ada
 help / color / mirror / Atom feed
From: "David C. Hoos, Sr." <david.c.hoos.sr@ada95.com>
Subject: Re: How do I pass a  pointer from a C function to Ada83?
Date: 1999/01/10
Date: 1999-01-10T00:00:00+00:00	[thread overview]
Message-ID: <eiRnBeJP#GA.227@pet.hiwaay.net> (raw)
In-Reply-To: 778sp0$50p$1@holly.prod.itd.earthlink.net


boolaz wrote in message <778sp0$50p$1@holly.prod.itd.earthlink.net>...
>I am trying to pass an array pointer from a C function to my Ada83 program.
>I can't seem to get the pragma interface, access type, and/or
system.address
>stuff to work.  Any suggestions?
>
You didn't specify which Ada83 compiler you're using, so there are some
details which you need to look up in Appendix F for your compiler.
The original Ada83 Standard did con define means to call Ada subprograms
from other languages, but many compilers did implement the feature.  It may
be that you need to use pragma Export, as pragma interface is for calling
other languages from Ada.

Since in C there aren't really any arrays (only address arithmetic), the
Ada83 type that's appropriate is usually System.Address;
You need, then, some way to let the Ada subprogram know about the array
bounds, and declare an Ada constrained array type which is appropriate.

Since I no longer have an Ada83 compiler on my systems at home, I built a
small example using GNAT, but only using Ada83 features.
Here's a small example for a two-dimensional array:

#include <stdlib.h>
int main (void)
{
  extern void show_matrix (double [ ] [], int rows, int cols);
  double matrix [3][4] =
  {
  {1.0, 2.0, 3.0, 4.0},
  {5.0, 6.0, 7.0, 8.0},
  {9.0, 10.0, 11.0, 12.0}
  };
#define COLS(m) (sizeof (m [0]) / sizeof (* * m))
#define ROWS(m) (sizeof (m) / (COLS (m) * sizeof (* * m)))
  adainit ();
  show_matrix (matrix, ROWS (matrix), COLS (matrix));
  adafinal ();
  return (EXIT_SUCCESS);
}
with Text_IO;
package body Matrices is

   package LFIO is new Text_Io.Float_Io (Long_Float);

   procedure Show_Matrix
   (Matrix_Address : System.Address;
    Rows           : Natural;
    Columns        : Natural)
   is
      type Matrix is array (0 .. Rows - 1, 0 .. Columns - 1) of Long_Float;
      The_Matrix : Matrix;
      for The_Matrix use at Matrix_Address;
   begin
      Text_IO.Put_Line ("Rows:" & integer'image (Rows));
      Text_IO.Put_Line ("Cols:" & integer'image (Columns));
      for R in The_Matrix'Range (1) loop
         for C in The_Matrix'Range (2) loop
            Text_IO.Put
              ("A[" & Integer'Image (R) & " ][" & Integer'Image (C) & " ]
=");
            LFIO.Put (The_Matrix (R, C));
            Text_Io.New_Line;
         end loop;
      end loop;
   end Show_Matrix;

end Matrices;

with System;
package Matrices is
   procedure Show_Matrix
     (Matrix_Address : System.Address;
      Rows           : Natural;
      Columns        : Natural);
   pragma Export (C, Show_Matrix, "show_matrix");
end Matrices;








      parent reply	other threads:[~1999-01-10  0:00 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-01-09  0:00 How do I pass a pointer from a C function to Ada83? boolaz
1999-01-10  0:00 ` Matthew Heaney
1999-01-10  0:00   ` David C. Hoos, Sr.
1999-01-10  0:00 ` David C. Hoos, Sr. [this message]
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox