Re: What is the best way to define the Imported C function



qunying wrote:

I am learning Ada and try to test the interface with C.

I would suggest that you learn Ada before trying to interface it to C. But if you are going to do it, use the types in Interfaces.C and its children, or types declared Convention C, not String and Integer; and don't pass explicit pointer values when Ada's interfacing rules will do the right thing for you.

for this function, what is the best way to define the function in Ada?

int xcb_parse_display(const char *name, char **host, int *display, int
*screen);

I can't help you (and really, neither can those who have replied to you) because the C specification isn't a specification; it doesn't tell us how the function uses its parameters. Without that information, we can't tell how to interface to it.

The problem is that C uses the same construct for many conceptually different things. For example:

void f (char* c);

may represent something that is conceptually equivalent to Ada's

procedure F (C : in Character); [unlikely, but possible]
procedure F (C : in out Character);
procedure F (C : out Character);
procedure F (C : in String);
procedure F (C : in out String);
procedure F (C : out String);

Given the common practice of using "char" to represent what Ada calls a Storage_Element, and "char[]" or "char*" for Storage_Array, this single C declaration may mean any of 12 different things.

--
Jeff Carter
"C++ is like jamming a helicopter inside a Miata
and expecting some sort of improvement."
Drew Olbrich
51
.



Relevant Pages

  • Re: Several questions about character C binding
    ... are actually arrays of the C type char that have a C null character ... interoperable with a C string. ... That says that the intent of the standard is that the above interface ... to happen when the C prototype has a pointer argument (as would be the ...
    (comp.lang.fortran)
  • Several questions about character C binding
    ... I have many questions about character variables passed to a C ... I tested an interface passing a TYPE. ... obligatory the use of the C_LOC function from the FORTRAN side: ... the dimension of the string behind the scene as an additional argument ...
    (comp.lang.fortran)
  • Re: Is there an end of string like in C
    ... In Ada you know the array bounds, and a string is an array of Character. ...
    (comp.lang.ada)
  • Re: String literals and wide_string literals - how?
    ... But I'll bet it _seems_ weird to anyone who doesn't know Ada well. ... Character, or derived from it. ... And STRING as array of char, although it ...
    (comp.lang.ada)
  • Re: Is there an end of string like in C
    ... You can define your own character types and your own string types. ... type String is array of Character; ... The C approach is referred to in Ada as a bounded string; the logical value can vary in length from zero to some maximum number of characters. ...
    (comp.lang.ada)

Quantcast