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 7492925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T16:39:54+00:00 2026-05-29T16:39:54+00:00

What is a good way to cast an Ada String to a System.Adress which

  • 0

What is a good way to “cast” an Ada String to a System.Adress which would be equivalent to casting char* to void* in C.

I am interfacing which a C library. A C type has a property which is of type void*, and users of the library typically assign the address pointed to by a C-string as this value. E.g:

struct my_type {
    void* value;
};

int main() {
    my_type t;
    t.value = "banana";
}

How can I achieve the equivalent in Ada, starting with an Ada String?

I’m using this technique at the moment, but it seems fishy to me.

declare
    str : constant String := "banana";
    data : constant char_array := To_C(str);
    mine : my_type;
begin
    mine.value := data(data'First)'Address;
end;

I’m OK with any solution, even Ada 2012.

  • 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-05-29T16:39:56+00:00Added an answer on May 29, 2026 at 4:39 pm

    You mention in a comment that you’re using void* “because it should be able to take the address of anything; not just a string.”

    So then, one has to ask how does the general-pointer translate into Ada, particularly in such a way as to take advantage of the typing and sub-typing features. I would submit that an “anything”, in this context, cannot be solved generally; that is to say, if you want to maintain the ‘flexibility’ of the construct you must sacrifice the advantages Ada provides with its type-system. Furthermore, I submit that presented as-is, it is generally impossible to reliably use for “anything”.

    I say this because there is no method of determining even the length of the contained “anything.” If it’s a string then the length is from the pointed-address, counting consecutively, until the first NUL character (ASCII 0). However, there is no method for determining the length if it is not a string (how would we know the length/size of array [1,2,3] or OBJECT)… and so we have no method for determining even the length of the “anything.”

    Determining the length is an important factor in writing stable/secure code, because if you don’t you’re inviting buffer overflows.


    But, leaving that off, if you can provide some information on the data, whether via parameter or changing my_struct, then we can use that information to build a better type-conversion. (In general, the more information you have about a type the better, because you can then check for validity-of-data in ways you couldn’t before; or better yet, have the compiler check that for you.)

    Type Data_Type is Array( Positive Range <> ) of Interfaces.Unsigned_8;
      For Data_Type'Component_Size Use 8;
    
    
    Function Some_Data( Stream : not null access Ada.Streams.Root_Stream_Type'Class; 
                        Length : In Positive ) Return Data_Type is
      begin
         Return Result : Data_Type(1..Length) do
            For Index in Result'Range loop
               Interfaces.Unsigned_8'Read(Stream, Result(Index));
            end Loop;
         End Return;
      end Some_Data;
    

    You can use the above to generate an array of 8-bit unsigned integers which would contain the data from the the stream. It outlines what you’d have to do in the general case, though since you’re working with C-imports what you can do is modify it a bit so that a) there is a Temp variable which is an array like Result but use For Temp'Address Use [...] to overlay it on the my_type.value and then use the for-loop to copy it out.

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

Sidebar

Related Questions

What would be a good way to determine if a string contains an IPv4
Good day all, what would be the best way to read a value from
I have an IEnumerable<KeyValuePair<string,string>> , from which I would like, ideally, an anonymous object
What is a good way to persist querystring values in asp.net mvc? If I
XmlAttributeOverrides is a good way to override some attributes for serialization. MSDN says: You
What's a good way to check if a package is installed while within a
Is there any good way of truncating text with plain HTML and CSS, so
Is there a good way in asp.net MVC to trace ModelState errors? IsValid returns
Is there a good way to get the debug information that Django provides when
Does anyone have a good way of deleting duplicate transactions (same date, amount, biller,

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.