Mail Archives: djgpp/1997/02/13/16:00:29
Bob Schultz (unicorn AT walrus DOT com) wrote:
: Tudor <tudor AT cam DOT org> wrote:
: >char string[5]="abcde" and
: >char *string="abcde" are equivalent.
:
: They are not at all equivalent. The first allocates storage for 5
: chars and initializes them to the chars 'abcde'. Note that the
: contents of the memory location after the 'e' is undefined. The
Except the '\0' after the 'e' I hope? No?
: second allocates space for a pointer to a char and then initializes
: the pointer to point to the null terminated string "abcde" somewhere
: else in memory. What is equivalent is the two different ways of
: referencing an element in the array or string. 'string[2]' is the
: same as '*(string+2)'. And even though it looks odd '[2]string' also
: references the same memory.
Yes, you're absolutely right about this. Many, _many_ programming errors
involve exactly _this_ equivalence-assumption (nice word).
Important difference: string[] is writable, *string is not.
It took me a while understanding the differences.
--
Groeten, Michel. http://www.cs.vu.nl/~mdruiter
\----/==\----/
\ / \ / "Life is cool.", Beavis.
\/ \/
- Raw text -