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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:02:28+00:00 2026-05-17T00:02:28+00:00

I’m writing a Unicode library for C as a personal exercise. Without the actual

  • 0

I’m writing a Unicode library for C as a personal exercise. Without the actual code, I have this:

typedef struct string {
    unsigned long length;
    unsigned *data;
} string;

// really simple stuff

string *upush(string *s, unsigned c) { ... }

// UTF-8 conversion

string *ctou(char *old) { ... }

char *utoc(string *old) { ... }

upush pushes the full Unicode code point c onto the end of s, reallocating as needed.

I can use the class like this, for example:

string a;
upush(&a, 0x44);
puts(utoc(&a));

Instead of having to use & to pass the reference to a function, is there a way I can write so that I can call:

string a;
upush(a, 0x44);
puts(utoc(a));

Or, do you think it’s just better to typedef string as a struct string pointer, not the struct itself, and always deal with struct string pointers?

Resolution? Thanks to Dummy00001, this is now my library’s general profile:

typedef struct string {
    unsigned long length;
    unsigned *data;
} string;

// really simple stuff

string *upush(string *s, unsigned c) { ... }

// UTF-8 conversions

string ctou(char *old) { ... }

char *utoc(string old) { ... }

Here are some examples:

string a = ctou("Hello, world");
upush(&a, '!');
puts(utoc(a));

I also learn, thanks to Doches, that in C++ (which is what I’ll write a wrapper for this library in), you can write the prototype like this to do implicit pass-by-reference:

string *upush(string &s, unsigned c);

For those who are interested, here’s my ongoing progress: http://delan.ath.cx/ulib

  • 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-17T00:02:29+00:00Added an answer on May 17, 2026 at 12:02 am

    Instead of having to use & to pass the reference to a function, is there a way I can write so that I can call:
    string a;
    upush(a, 0x44);
    puts(utoc(a));

    I personally prefer the & notation and use it (even in C++) to differentiate between the case of function which might modify its argument and the case of function accessing the struct as a const. IOW:

    string a;  
    upush(&a, 0x44);  // pass pointer, we modify the "a"
    puts(utoc(a));    // pass "a" as a copy, utoc() doesn't modify it
    

    In code above one sees immediately that upush() takes &a and thus potentially modifies it. While the utoc() takes the a and doesn’t modify it.

    But the final decision how to make the interface largely depends on how you are going to manage the memory.

    Or, do you think it’s just better to typedef string as a struct string pointer, not the struct itself, and always deal with struct string pointers?

    Make the symbol string being a pointer to an opaque structure, defined only inside the library itself. In public interface header:

    struct string_s;
    typedef struct string_s *string;
    

    In internal header:

    struct string_s {
       unsigned long length;
       unsigned *data;
    };
    

    Interface functions would only accept the string which is pointer. Provide functions to create new string (calloc(1,sizeof(string *))) and to destroy it (free()). That way you would provide an interface to application which is consistent (developer doesn’t have to think about what is pointer what is not) and is also resilient to the library internal changes (since the struct’s structure only known inside the library itself).

    Downside is that one need to use dynamic memory management to allocate the puny struct: malloc()/free() are fast, but it still fells like an overkill.

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

Sidebar

Related Questions

Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have a French site that I want to parse, but am running into
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.