www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/10/16/19:41:39

Date: Thu, 16 Oct 1997 16:39:56 -0700 (PDT)
Message-Id: <199710162339.QAA04756@adit.ap.net>
Mime-Version: 1.0
To: Thomas Demmer <demmer AT LSTM DOT Ruhr-UNI-Bochum DOT De>, djgpp AT delorie DOT com
From: Nate Eldredge <eldredge AT ap DOT net>
Subject: Re: Is this a bug ??? or feature ??

At 08:23  10/16/1997 +0200, Thomas Demmer wrote:
>John M. Aldrich wrote:
>[...]
>> However, I must say again that you should NOT overwrite string constants
>> without a good reason; it's terrible programming.  Unless this is the
>> exact intent of whatever project you are working on, there must be a
>> better way to do what you want.  If you describe your goals, maybe we
>> can suggest something.
>> 
>Hmm, maybe I don't get the point here, but I frequently do things like
>
>char inifile[256]="foo.bar";
>
>and in a parse routine with getopt
>  
> switch( c=getopt(argc, argv, "n:")){
>   case 'n':  strcpy(inifile, optarg);
>	      break;
>  ...
>
>Is this one of the rare occasions where char[] and *char are
>quite different things?
Yes. This code:
char string[XXX] = "hello";
and this:
char *string = "hello";
are different. The first allocates an array. Its size is whatever you
specify, or the size of the initializer if you don't specify, and it is
initially filled with "hello". In the second case, only a pointer is
allocated. The compiler creates the string literal, a constant, and puts it
in a read-only section by default. Then it gives the variable `string' an
initial value which is the address of the string literal. So one is an array
initializer and one is a string literal. I agree, the syntaxes (syntaces?)
are confusingly similar.

Hope that wasn't too confusing :)

Nate Eldredge
eldredge AT ap DOT net



- Raw text -


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