Ask your own question, for FREE!
Computer Science 22 Online
OpenStudy (anonymous):

I want to write my own C string library. What should I do? I want it to be able to do everything the C standard library functions can do, but quicker and safer. It needs to be portable and easy to use and bug-free as well

OpenStudy (anonymous):

So far, I've started with typedef struct gstring { char* data; size_t length; } gstring; // G for george, not the underwear!

OpenStudy (farmdawgnation):

Why are you looking to recreate the C string library? I mean, it's pretty battle tested as it is.

OpenStudy (farmdawgnation):

Or is this strictly a learning exercise?

OpenStudy (anonymous):

It is a rite of passage :-D

OpenStudy (anonymous):

The next thing I'd do is add constructors and start overloading operators. Assignment operators, concatentation with +, conversion operator to char*, and stream insertion and extraction operators.

OpenStudy (anonymous):

If only C supported operator overloading :( How am I going to print my gstrings though?

OpenStudy (anonymous):

I guess I will be using the plain write() functions instead of the printf family :( but how will I do formatted input/output

OpenStudy (anonymous):

Ah, C, not C++ - my mistake ;) So, in lieu of operators, you'll need standalone functions for concatenation and similar. For printing, you could just printf("String: %s", myGString.data), or write a new function printfgstring that can take a gstring as its sole argument, or even decomposes a variable argument list and gets the c-string out of your gstring struct.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!