简体   繁体   中英

What third-party options are there for working with strings in C?

I've been aware for awhile that I code about 17x faster in Python than in C, and I guess I sort of assumed I wasn't much of a programmer until I really thought about it and realized that the only problem is that I can't handle C strings/char arrays/char pointers/whatever. I have a block about them, and manipulating them takes me hours. I do not have this problem in C++.

But life forces me to code in pure C at the moment, and I'm trying to find if there's some third-party library I can add that will make my life easier. I did some googling and I didn't really find much. I'm starting to consider making it myself, but I feel like I'd be reinventing the wheel. Surely there's something out there? Does anyone know of something like that? Something that makes C a little more like C++ in that respect. I realize that's a silly idea, since that's the point of C++, but you understand, I'm sure.

有一个更好的字符串库 ,它是一个纯C库,用于处理“字符串”,试图避免标准C字符串处理的许多问题。

http://developer.gnome.org/glib/2.34/glib-utilities.html

http://developer.gnome.org/glib/2.34/glib-Strings.html

It's the foundation of gnome, but you can use it independently from gnome, I think. According to the overview page, "It works on many UNIX-like platforms, as well as Windows and OS X."

You may begin resolving your pointer/array/string problem by reading the related questions on SO. There's no shortage of such questions.

There are a few important things that you must learn and understand in order to write correct C code containing pointers/arrays/strings:

  1. Declarations of pointers, arrays, pointers to pointers, pointers to arrays, arrays of arrays, arrays of pointers and so on and so forth. C declarations are truly odd to the uninitiated. But, they are learnable and there are some tools like cdecl that can quickly translate abracadabrish declarations into plain English or the other way around.
  2. If you see that a function declares one of its parameters as an array (eg void f(int a[]) ), don't believe your eyes. It's a pointer, not an array. In C, arrays are never passed as function arguments. Only pointers are passed. The syntax is deceptive and the creators of C have been/are sorry about things like this, but that's all history now. You can't change it.
  3. In all C expressions (not to be confused with declarations and definitions), except about one, arrays behave as pointers to their first element. The exception is the sizeof operator. sizeof(array) will return you the true array size in chars . But the moment you do sizeof(array+0) , you have converted array to an expression of pointer type pointing to array[0] and in this case sizeof will return you the size of the pointer.
  4. You cannot assign arrays. You can initialize them when you define them, you can copy them, but not assign with = after they have been defined.
  5. There are no strings in C in the form of a fundamental type. There are only chars , arrays of chars and pointers to chars and pointers to arrays of chars and we often refer to all of them as strings, but they really aren't. The thing that you see as a string, eg "I am a string. Or maybe I'm not." is called a string literal and somewhere there is an array of chars associated with it. I say somewhere, because the string literal behaves differently in different contexts. In all C expressions (not to be confused with declarations/definitions), except about one, string literals behave as a pointer to the first char of an array containing the literal's text. The exception is, again, sizeof . It will return you the size of the underlying array of chars in something like sizeof("ABC") . Again, just like with arrays, the moment you write sizeof("ABC"+0) , you have converted "ABC" to a pointer to char and sizeof will return you the size of the pointer.
  6. You must not attempt to modify the char arrays created by string literals: "ABC"[0] = 'Z'; is undefined behavior. And so is char* p = "ABC"; p[0] = 'Z'; char* p = "ABC"; p[0] = 'Z'; or the equivalent char* p = "ABC"; *p = 'Z'; char* p = "ABC"; *p = 'Z'; .
  7. You may use string literals to initialize arrays of chars or pointers to chars (you can also assign string literals to pointers, but not to arrays, arrays aren't assignable, as pointed out earlier). What happens depends on what you're initializing. In char a[] = "Hello"; or in char a[] = { "Hello!" }; char a[] = { "Hello!" }; you create an array of chars and you set its contents to be the text in the string literal. And you can modify that array afterwards if you need to. In char* p = "World!"; you create a char array containing the text of the literal string and you create a pointer to a char pointing to the first char of that array. In this case you cannot alter the array as I pointed out earlier.
  8. Two or more adjacent string literals delimited by space merge into one: "Hello" " World!" is the same as "Hello World!" .
  9. There's also pointer arithmetic, but it's easy.

This is about it. Master declarations, master arrays in expressions and function parameters, watch your string literals.

While re-inventing the wheel is not always desired. I tend to think that by doing so you get a greater understanding for how things work. Though, not sure what type of time-frame you're working in. Otherwise, as mentioned in another answer Better String Library is a very good one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM