Sign in to follow this  

Please, rate my article.

This topic is 4867 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Thanks Sneftel, I think the book that I read is outdated.

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.

Share this post


Link to post
Share on other sites
Quote:
Original post by Maquiavel
Thanks 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.

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."

also you should remove the cast to (char**), it's not needed.

Share this post


Link to post
Share on other sites

This topic is 4867 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this