Colin Ray
Colin Ray

Reputation: 175

Passing a C++ array to Ada95

I'm trying to pass an array of unsigned integers from C++ to Ada. The Ada Lovelace tutorial states that an Ada array corresponds to a pointer to the first element of an array in C++.

Here is what I'm trying to do.

C++

unsigned int buffer[bufferSize];

...

unsigned int* getBuffer() {
    return buffer;
}

Ada

pragma Import (C, C_Get_Buffer, "getBuffer");

...

function C_Get_Buffer returns System.Address;

...

Buffer : array (1 .. Buffer_Size) of Interfaces.C.Unsigned;

...

Buffer'Address := C_Get_Buffer;

I'm finding that Buffer'Address cannot be assigned however. What is the correct way to go about passing an array from C to Ada?

Thanks!

Upvotes: 4

Views: 1243

Answers (1)

Simon Wright
Simon Wright

Reputation: 25491

This will do as you ask (I didn’t bother with Buffer_Size):

function C_Get_Buffer return System.Address;
pragma Import (C, C_Get_Buffer, "getBuffer");
Buffer_Address : constant System.Address := C_Get_Buffer;
Buffer : array (1 .. 10) of Interfaces.C.unsigned;
for Buffer'Address use Buffer_Address;

However, this might be appropriate as a shorter way of achieving the same thing:

Buffer : array (1 .. 10) of Interfaces.C.unsigned;
pragma Import (C, Buffer, "buffer");

Upvotes: 3

Related Questions