Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8137715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T11:14:27+00:00 2026-06-06T11:14:27+00:00

I have a C function int func( int a, int* b) that I need

  • 0

I have a C function int func( int a, int* b) that I need to import and use in Ada 95. Where the C function would typically be called in C as c = func(a, &b);

I import C functions all the time into Ada, but have always avoided using functions with pass by reference arguments, but it’s finally time to learn.

I would like to know how to declare this function in Ada, and would also like a quick example of using it with declared variables shown (because I’m still a little fuzzy on the Access types).

Thanks all!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T11:14:36+00:00Added an answer on June 6, 2026 at 11:14 am

    In C, int* b can mean a lot of things. Perhaps it’s really just a pointer to one variable, but it may also be an array for which int a is the length. This code assumes that int* b is actually just a value that’s passed by reference:

    with Interfaces.C;
    
    -- ...
    package C renames Interfaces.C;
    
    function Func (A : C.int; B : access C.int) return C.int;
    pragma Import (Convention => C, Entity => Func,
                   External_Name => "func");
    
    -- ...
    
    declare
       A : C.int := 42;
       B : aliased C.int := 23;
       C : C.int;
    begin
       C := Func (A, B'Access);
       -- ...
    end;
    

    You can use 'Access on aliased variables. This is safe as long as you’re sure that the pointer won’t be stored on the C side and accessed after the end of life of variable B. (If the C declaration uses the const keyword, you can use access constant on the Ada side, but that’s Ada 2005 only.)

    You can also use a named type:

    -- ...
    type Int_Access is access C.int;
    function Func (A : C.int; B : Int_Access) return C.int;
    -- ...
    C := Func (A, B'Unchecked_Access);
    -- ...
    

    Now we need to use 'Unchecked_Access because Ada normally does not allow a non-local access type (as Int_Access) to refer to a local variable. If you know what the C code will do with the pointer (like you should), you can use named types to specify that no references to local variables should be passed.

    Nota bene 1: If you have a procedure (in C: a function that returns void), you can specify a variable to be passed by reference by using in out in your Ada procedure declaration instead of access. This way, you do not need to worry about access types at all. As before, you need to be sure that the pointer is not stored at the C side.

    Nota bene 2: Record types and arrays are passed by reference anyway – unless you specify pragma Convention (C_Pass_By_Copy, Your_Type);. This is a common gotcha when wrapping C functions in Ada.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function that requires a function pointer as argument: int func(int a,
Suppose I have a function that allocates memory for the caller: int func(void **mem1,
I need a function wrapper for std::bind that will be called before the function
I have a struct that takes a function pointer, like this: typedef int (*node_transition_func)(
Suppose we have an array say: int arr[1000]; and I have a function that
I have a simple function that uses a callback and I want use functor
Suppose an int[] is passed in, and I have function f(int[] array), if I
Ok so im sure im doing something stupid :D i have a function: int
I have a function m(int i, char c) which takes and returns a char
I have a C function with the following signature: int my_function(int n, struct player

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.