问题
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.
回答1:
There is the Better String Library, which is a pure C library for working with "strings" which tries to avoid many of the issues with standard C string handling.
回答2:
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."
回答3:
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:
- 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. - If you see that a function declares one of its parameters as an array (e.g.
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. - 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 inchars
. But the moment you dosizeof(array+0)
, you have convertedarray
to an expression of pointer type pointing toarray[0]
and in this casesizeof
will return you the size of the pointer. - 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. - There are no strings in C in the form of a fundamental type. There are only
chars
, arrays ofchars
and pointers tochars
and pointers to arrays ofchars
and we often refer to all of them as strings, but they really aren't. The thing that you see as a string, e.g."I am a string. Or maybe I'm not."
is called a string literal and somewhere there is an array ofchars
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 firstchar
of an array containing the literal's text. The exception is, again,sizeof
. It will return you the size of the underlying array ofchars
in something likesizeof("ABC")
. Again, just like with arrays, the moment you writesizeof("ABC"+0)
, you have converted"ABC"
to a pointer tochar
andsizeof
will return you the size of the pointer. - You must not attempt to modify the
char
arrays created by string literals:"ABC"[0] = 'Z';
is undefined behavior. And so ischar* p = "ABC"; p[0] = 'Z';
or the equivalentchar* p = "ABC"; *p = 'Z';
. - You may use string literals to initialize arrays of
chars
or pointers tochars
(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. Inchar a[] = "Hello";
or inchar a[] = { "Hello!" };
you create an array ofchars
and you set its contents to be the text in the string literal. And you can modify that array afterwards if you need to. Inchar* p = "World!";
you create achar
array containing the text of the literal string and you create a pointer to achar
pointing to the firstchar
of that array. In this case you cannot alter the array as I pointed out earlier. - Two or more adjacent string literals delimited by space merge into one:
"Hello" " World!"
is the same as"Hello World!"
. - 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.
回答4:
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.
来源:https://stackoverflow.com/questions/12718734/what-third-party-options-are-there-for-working-with-strings-in-c