Reputation: 37098
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.
Upvotes: 3
Views: 460
Reputation: 62106
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:
cdecl
that can quickly translate abracadabrish declarations into plain English or the other way around.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.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.=
after they have been defined.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, e.g. "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.char
arrays created by string literals: "ABC"[0] = 'Z';
is undefined behavior. And so is char* p = "ABC"; p[0] = 'Z';
or the equivalent char* p = "ABC"; *p = 'Z';
.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!" };
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."Hello" " World!"
is the same as "Hello World!"
.This is about it. Master declarations, master arrays in expressions and function parameters, watch your string literals.
Upvotes: 2
Reputation: 1889
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."
Upvotes: 3
Reputation: 503
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.
Upvotes: 0
Reputation: 564771
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.
Upvotes: 6