16

I have used several dynamically typed languages and I have been avoiding C but enough is enough, it's the right tool for the job sometimes and I need to get over it.

The things I miss working with C are associative arrays and large string libraries. Is there a library that gives more options then string.h? Any general advice when it comes to make the transition with strings?

7
  • 1
    are you absolutely restricted to C? C++ has Boost (boost.org/doc/libs/1_46_1/doc/html/string_algo.html). Jun 7, 2011 at 21:37
  • 2
    Aren't the things you're talking about the reasons you would use a different language than C?
    – Carl Norum
    Jun 7, 2011 at 21:38
  • Hi Andrew C++ scares me. I understand it but I think it will take me years to be fluent in it-Patrick
    – Patrick
    Jun 7, 2011 at 21:39
  • 2
    @Patrick: You gotta get over that mentality, or you won't survive as a developer. Languages are tools. Nothing more.
    – Alan
    Jun 7, 2011 at 21:40
  • 4
    @Alan: True, but the OP is correct when he says "it will take years to be fluent in it". Jun 7, 2011 at 21:45

7 Answers 7

10

You can take a look at the Better String Library. The description from the site:

The Better String Library is an abstraction of a string data type which is superior to the C library char buffer string type, or C++'s std::string. Among the features achieved are:

  • Substantial mitigation of buffer overflow/overrun problems and other failures that result from erroneous usage of the common C string library functions
  • Significantly simplified string manipulation
  • High performance interoperability with other source/libraries which expect '\0' terminated char buffers
  • Improved overall performance of common string operations
  • Functional equivalency with other more modern languages

The library is totally stand alone, portable (known to work with gcc/g++, MSVC++, Intel C++, WATCOM C/C++, Turbo C, Borland C++, IBM's native CC compiler on Windows, Linux and Mac OS X), high performance, easy to use and is not part of some other collection of data structures. Even the file I/O functions are totally abstracted (so that other stream-like mechanisms, like sockets, can be used.) Nevertheless, it is adequate as a complete replacement of the C string library for string manipulation in any C program.

0
5

POSIX gives you <string.h>, <strings.h> and <regex.h>. If you really need more of a string library than this, C is probably not the right tool for that particular job.

As for a hash table, you can't get a type-safe hash table in C without a lot of nasty macros. If you're OK with just storing void-pointers, or with doing some manual work for each type of map, then you shouldn't be lacking for options. Coding your own hash table is a hoot and a half - just search Stackoverflow for help with the hash function. If you don't want to roll your own, strmap [LGPL] looks decent.

0
2

GLib provides many pre-made data structures and string handling functions, but it's a set of functions and types completely separated from the "usual" ones, and it's not a very lightweight dependency.

If instead C++ is a viable alternative for your task, it bundles a string class and several generic containers ready-made into the standard library (and much other related stuff can be found in Boost).

2
  • 1
    I know that the last paragraph will bring the downvotes of the hardcore C users. :) Jun 7, 2011 at 21:39
  • 1
    I think the first paragraph (recommending glib) would bring more downvotes from hardcore C users, but I'll refrain... :-) Jun 7, 2011 at 22:16
1

What specifically are you looking for in your extended c-string library?

One way to get better at C, is to create your own c-string library. Then make it open source, and let others help refine it.

I don't usually advocate creating your own string libaries, but w.r.t. C, it's a great way to learn C.

2
  • Hi Alan I actually don't really have a problem to solve, just trying to get my head around the language, thanks again
    – Patrick
    Jun 7, 2011 at 21:43
  • 1
    Actually I think making your own string (or whatever data structure) library is about the worst approach to learning C. It's a good way to learn about implementing higher-level languages, but if you really want higher-level general-purpose data structures, you should probably be using an existing higher-level language. The only time C shines is when you actually use C idioms, and "high level strings" is not a C idiom. Jun 7, 2011 at 22:18
1

I've not used it myself, but you should at least review the SEI/CERT library Specifications for Managed Strings, 2nd Edition. The code can be found at CERT.

0
1

Much of the power of C consists of the ability to have direct control over the memory as a sequence of bytes. It is a bit against the philosophy of the language to treat strings as something higher-level than that.

I would recommend rolling your own very basic one. It will be an enlightening experience especially to learn pointer arithmetics and loops.

For example, learn about "Schlemiel the Painter's algorithm" regarding strcat and design your library to solve this problem.

0
0

An associative array associating string keys and struct values in C consists of:

  • A hash function for strings
  • An array with a prime number of elements, inside each of which is a linked-list head.
  • Linked-list elements containing char * pointers to the stored keys and (optionally) a struct * pointer to the corresponding value for each key.

To store a string key in your associative array:

  • Hash it modulo that prime array size.
  • In that array bin, add it to the linked-list.
  • Assign the value pointer to the value you are adding.
1
  • @Patrick, I hope you understand what I'm getting at: roll your own. It's a small file when you're done. Jun 8, 2011 at 16:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.