4 ms·
"In C an array is generally just a pointer to the first element." No, arrays are not pointers. "A String is generally a pointer to a series of bytes (chars) w
by _kst_ 8y ago
"In C an array is generally just a pointer to the first element."
No, arrays are not pointers.
"A String is generally a pointer to a series of bytes (chars) which ends in a byte value of 0 to indicate the end of the string."
No, a string is by definition "a contiguous sequence of characters terminated by and including the first null
character". It is not in any sense a pointer.
Anyone who things arrays or strings are "really" just pointers should not be writing C tutorials.
(See section 6 of the comp.lang.c FAQ, http://www.c-faq.com/ http://www.c-faq.com/ .)
- AceJohnny2 8y agoIndeed, "arrays are pointers" is a polite fiction we use to more easily pass array references around, but there are subtle differences that the compiler usually hides from you. Until it doesn't.
- _kst_ 8y agoI wouldn't call "arrays are pointers" a polite fiction. I'd call it a dangerous misconception that makes it more difficult to understand what's really going on. The language is partly at fault for a couple of rules that implicitly convert or "adjust" arrays to pointers in certain contexts, but the solution is to explain the differences, not to gloss over them.
- webkike 8y agoExactly. I think this point is most easily observed through extern.
- szemet 8y agoOk. But I do not blame people who unify the two things, because C does not really stress the difference. On the contrary - even in small details, like for example the equivalence of p[i] and * (p+i), so you can legally write 3[p] in C...(==p[3])
- simias 8y agoIt's because that C makes it easy to confuse the two concepts that it's very important to stress when and how they're different. What creates the confusion in C is that arrays try very hard to decay into pointers at any occasion and on top of that you have "fake" arrays when they're declared as function parameters (something that's probably the most insane "feature" of C IMO. And don't get me started to the a[static n] syntax that solves nothing and introduces even more confusion on top of adding yet an other completely new meaning to the 'static' keyword). Most crucially they behave very differently when it comes to using sizeof. It's also very important to understand the difference to be able to understand `const char p = "abcd"` vs `char p[] = "abcd"` or `struct s { / ... /; char data; }` vs. `struct s { /* ... */; char data[]; }`.
- _kst_ 8y agoC really does stress the difference. Look up the definitions of "pointer" and "array" in the C standard. They're entirely distinct concepts. p[i] is equivalent to (p+i), and that's possible because both the indexing "[]" operator and pointer addition "+" operator require a pointer and an integer as operands. (The pointer needs to point to an element of an array object, but that's not enforced at compile time.) For p[i], it's very common for the pointer operand to be an array expression that "decays" to a pointer. (3[p] is valid because pointer addition, like integer and floating-point addition, is commutative. It could* have been defined to require a pointer as the left operand and an integer as the right operand. The decision was made when the distinction between integers and pointers wasn't as strong as it is in modern C.)
- foota 8y agoCurious, where are you quoting from on the definition of a string? I wouldn't imagine the C standard differentiates strings from other pointer types other than when talking about string literals.