Recommended Posts

Hello,
I've written an article about C Strings don't for publish it, but in case of I forget this information one day.
The article is here: Clicky
Please, say if I've written some wrong information, what I need to change, etc.
Thanks
Alfred
p.s.: Sorry for my poor English.

Share this post

Link to post

Share on other sites

Your explanation is not bad, but you make the same mistake as many beginning programmers, of equating arrays and pointers.

This:char str[] = "foo";

is not the same as:char *str = "foo";

The first creates a mutable array of 4 characters, and fills it with the elements 'f', 'o', 'o', and '\0'. The second creates a pointer and makes it point to a location in memory where the string "foo" is located. The latter is immutable; you are not allowed to change the string in any way. The former is mutable, and is allocated on the stack (as long as it's local to a function). The capabilities of the two are thus vastly different.

Share this post

Link to post

Share on other sites

Original post by MaquiavelThanks Sneftel, I think the book that I read is outdated.

This is the way it has always been.

Quote:

And what about the strings' array? I wrote well? Is really that information?Because in true code it works well, with no compiler warnings and things like this.

Looks fine, except for the continued confusion between arrays and pointers.

One of the most insidious things is that C compilers generally won't complain about you modifying static strings, or returning a pointer to a stack-allocated string. So if you don't understand the difference, hard-to-find and hard-to-understand bugs will crop up.

0

Share this post

Link to post

Share on other sites

You should talk about passing pointers to arrays with and without the const keyword.

Also some of it is confusing and hard to understand.. this for example "you won't have a pointer to the desired string, but in fact, the pointer that point where is the pointer of the string you want."