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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:05:56+00:00 2026-05-26T12:05:56+00:00

I love Pascal for its clarity, so believe what the following code snippet along

  • 0

I love Pascal for its clarity, so believe what the following code snippet along with question title is self-explanatory:

procedure TForm1.FormClick(Sender: TObject);
const
  N = 42; { fnord }
type
  { this structure merely defines memory layout }
  TStringStruct = record
    NumberOfCharacters: Cardinal;
    { this array supposed to be friendly for the string type }
    StringCompatibleArray: array [0..N-1] of Char;
  end;
  { actual work is done with pointer to that structure }
  PStringStruct = ^TStringStruct;
var
  StringStruct: PStringStruct;
  S: string;
begin
  StringStruct := PopulatedElsewhere;

  { most pleasant code but will copy no more than N characters }
  S := StringStruct^.StringCompatibleArray;

  { this construct works but is way too ugly and complex }
  SetString(
    S,
    { in particular: must reference the array and then typecast to make it work }
    { default $T- state assumed, unfortunately $T+ has global effect and not useful here }
    PChar(@StringStruct^.StringCompatibleArray),
    StringStruct^.NumberOfCharacters
  );
end;

In case someone wants formal question: i’d like to see which options i have to perform such assignment, preferably less obscure than shown SetString call.

Note: i know what dereference operators are optional with structured types.

  • 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-26T12:05:57+00:00Added an answer on May 26, 2026 at 12:05 pm

    SetString is the way to go, ordinarily. It’s only obscure if people continue to remain ignorant about its usefulness. The type casting is necessary because there are two overloads, and the char array doesn’t exactly match either expected argument type (PAnsiChar and PWideChar).

    It’s wordy, but in your case it’s easily wrapped into a function for your data type, like ToString. As David suggested in a comment, you can let that function be the Implicit operator, and then you get the conversion automatically:

    class operator TStringStruct.Implicit(const Value: TStringStruct): string;
    begin
      SetString(Result, Value.StringCompatibleArray, Value.NumberOfCharacters);
    end;
    
    S := StringStruct^;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I love Ruby and its framework, but I don't think that Ruby On Rails
I love MXML but am starting to notice some of its drawbacks. The main
I love cucumber, and its table diff feature. But I, often use a td
I love designing my userinterfaces in qtdesigner because its just so damn simple. However,
I love my GUID. However, lately I have been doing some research to understand
I love paredit. But there are a couple of things I hate, and have
Would love some opinions on this problem I'm trying to workout. I'm trying to
I love Jenkins and appreciate that it is an active project. Still, I don't
I love LINQ to SQL but it has been bugging me that in using
I love NHibernate's ability to have one table to store multiple types based on

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.