A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

anonymous

  • 4 years ago

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 <string.h> functions can do, but quicker and safer. It needs to be portable and easy to use and bug-free as well

  • This Question is Closed
  1. anonymous
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  2. farmdawgnation
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  3. farmdawgnation
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Or is this strictly a learning exercise?

  4. anonymous
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    It is a rite of passage :-D

  5. anonymous
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  6. anonymous
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  7. anonymous
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  8. anonymous
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  9. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.