6 ms·
Well, it would be fairly inconvenient to have to type strcmp(&some_name[0], &some_other_name[0]) all the time. Or having to type const char ∗s = &"some constant
by Erwin 12y ago
Well, it would be fairly inconvenient to have to type strcmp(&some_name[0], &some_other_name[0]) all the time. Or having to type const char ∗s = &"some constant_string"[0] -- the type of a string literal is after all "char[size]".
As to your other point: it's legal for a pointer to an object to point one item past the end of the object. You may not read/write to this of course, but if you allocate 100 bytes of memory you may have char ∗start, and char ∗end = start+100. That's a nice concept taken to its limit in STL where all algorithms operate on abstract start; end+1 iterator pairs. So an empty sequence has start == end.
(Thanks to a sibling poster for the usage of ∗).
- Ded7xSEoPKYNsDd 12y agoWell, you can declare your variable as const char s[] = "...". And if strncmp were to be defined today, it really should have a prototype of int strncmp(size_t n, const char s1[static n], const char s2[static n]). This is valid C99, although it seems nobody writes code like this and prefers to degrade their arrays to pointers. (Today's C also has pointers to arrays, so the need for dynamic allocation is no valid excuse either.)