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

  • SEARCH
  • Home
  • 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 331297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T09:45:41+00:00 2026-05-12T09:45:41+00:00

I’m converting the header files of a C library to D modules, and was

  • 0

I’m converting the header files of a C library to D modules, and was wondering how I should handle C strings.

Using DMD 1, this works:

void f(char* s);  // Definition for C library's function.

But using DMD 2 (which I personally use, but I would like the modules to work for both) strings are const, so to get the same code using the modules to work requires

void f(const(char)* s);  // Definition for C library's function.

What should I do? Just use char* and make the ‘client’ code make the strings mutable somehow? Or modify the type depending on the version of the compiler compiling the code? If the former, what’s the best way to make them mutable? I thought .dup would do it, but the compiler wasn’t having a bar of it. If the latter, how would I go about doing it? I tried this:

version (D_Version2) {
    alias const(char)* charptr;
} else {
    alias char* charptr;
}

void f(charptr s);

But alas, the DMD 2 version isn’t valid code for DMD 1, and all code in version blocks must be valid code for the compiler compiling the code, even if the code wouldn’t be included in the resulting executable. So currently the code compiles in both, but you have to modify the alias first which, as you can imagine, isn’t ideal.

  • 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-12T09:45:41+00:00Added an answer on May 12, 2026 at 9:45 am

    You can use the mixin construct to use language-version-specific code that isn’t valid in all versions. Example:

    static if(version_major<2)
    {
        alias char* charptr;
    }
    else
    {
        mixin("alias const(char)* charptr;");
    }
    

    Regarding your actual question, I would suggest doing the same as when interfacing C libraries with C++ – define a type that’s const(char)* for D2 and char* for D1, but only use it when appropriate (for example, if a function takes a char* for a buffer to write to, it probably wouldn’t be appropriate to name const(char)* something as generic as “charptr”). LPCSTR could work 😉

    I didn’t understand the “What’s the best way to make them mutable” question.

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

Sidebar

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.