www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/02/13/16:00:29

From: mdruiter AT cs DOT vu DOT nl (Ruiter de M)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Question on pointers and arrays
Date: 12 Feb 1997 13:25:51 GMT
Organization: Fac. Wiskunde & Informatica, VU, Amsterdam
Lines: 28
Message-ID: <5dsgcv$5m4@star.cs.vu.nl>
References: <32f92a6c DOT 0 AT ntnews DOT compusmart DOT ab DOT ca> <32FA7146 DOT 3883 AT cam DOT org> <32fa7242 DOT 998097 AT news DOT walrus DOT com>
NNTP-Posting-Host: kits.cs.vu.nl
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019