Please, rate my article.

Started by
5 comments, last by soundsystem 19 years, 8 months ago
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.
Alfred Reinold Baudisch[Game Development Student] [MAC lover] [Ruby, Ruby on Rails and PHP developer] [Twitter]
Advertisement
Your link is broken.
Quote:Original post by Sneftel
Your link is broken.


Sorry, I corrected it now.
Alfred Reinold Baudisch[Game Development Student] [MAC lover] [Ruby, Ruby on Rails and PHP developer] [Twitter]
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.
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.
Alfred Reinold Baudisch[Game Development Student] [MAC lover] [Ruby, Ruby on Rails and PHP developer] [Twitter]
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.
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.

This topic is closed to new replies.

Advertisement