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