www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/02/17/21:24:43

From: mdruiter AT cs DOT vu DOT nl (Ruiter de M)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Question on pointers and arrays
Date: 14 Feb 1997 10:33:34 GMT
Organization: Fac. Wiskunde & Informatica, VU, Amsterdam
Lines: 104
Message-ID: <5e1f1u$m07@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> <5dsgcv$5m4 AT star DOT cs DOT vu DOT nl> <3303DF62 DOT 21E0 AT cs DOT com>
NNTP-Posting-Host: sloep33.cs.vu.nl
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

John M. Aldrich (fighteer AT cs DOT com) wrote:
: Ruiter de M wrote:
: > 
: > : >char string[5]="abcde" and
: > : >char *string="abcde" are equivalent.

That's not what _I_ (mdruiter) wrote :)

: Here's something interesting... those two definitions are technically
: equivalent in function because of variable alignment in memory.  Take a
: look:
: 
: char string[5] = "abcde";
: 
: This actually allocates eight bytes, not five, because of memory
: alignment.  'string' becomes an alias for the address of those eight
: bytes.  The null at the end of the string will get put in the sixth
: byte, which, even though it was not actually allocated as part of the
: string, is guaranteed by the alignment to not be touched by any other
: variable.  So even though this is completely wrong in form, it should
: never cause a crash.  If you tried "char string[8] = "abcdefgh";"
: though...
:
: char *string = "abcde";
: 
: This allocates two entities:  'string', which is a 4-byte pointer, and
: the string "abcde" itself, which is located somewhere in the program's
: static data space, and is also rounded up to eight bytes.
:: 
: Despite these differences, both can be referenced identically within the
: program.

I think not, see below.

: > Important difference: string[] is writable, *string is not.
: 
: Not true.  Both refer to a single character in memory, which the
: compiler is more than happy to let you modify.  The only thing the
: compiler will complain about is trying to change the value of string
: itself, if it is allocated as an array.  Of course, if you try to write
: to the pointer-allocated string, you are changing your program's static
: data space, which can have unpredictable effects if there are any other
: variables in your program that also refer to that same string.

Yes, you say _static_ data space. Which means it is not at all writable.
For instance:
-----
#include <stdio.h>

char string1[5]="abcde";
char *string2="abcde";

int main(void)
{
  printf("Here before the first try:\n");
  string1[0] = 'z';
  printf("Here before the second try:\n");
  string2[0] = 'z';
  printf("All worked out well.\n");
  
  return 0;
}
-----
Gives the following (at least on a SPARCstation with Solaris, I don't have
-----
DJGPP ready here.):
Here before the first try:
Here before the second try:
Segmentation Fault
-----
Not unpredictable at all. string1 is writable, string2 is not. Or am I getting
something wrong?
Maybe you mean there is no difference in _pointer_, which I think is not
true either.
For instance:
-----
#include <stdio.h>

char string1[5]="abcde";
char *string2="abcde";

int main(void)
{
  printf("Here before the first try:\n");
  string2 = NULL;
  printf("Here before the second try:\n");
  string1 = NULL;
  printf("All worked out well.\n");
  
  return 0;
}
-----
This doesn't compile, giving:
-----
test.c: In function `main':
test.c:11: incompatible types in assignment
-----
No error for string2, error for string1.
So. Different pointers to different memory. Or am I getting it wrong again?
-- 
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