Passing a C++ array to Ada95

纵然是瞬间 提交于 2019-12-23 21:09:20

问题


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!


回答1:


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");


来源:https://stackoverflow.com/questions/8436417/passing-a-c-array-to-ada95

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!