Date: Sun, 09 Sep 2001 19:35:51 +0300 From: "Eli Zaretskii" Sender: halo1 AT zahav DOT net DOT il To: djgpp AT delorie DOT com Message-Id: <1659-Sun09Sep2001193551+0300-eliz@is.elta.co.il> X-Mailer: Emacs 20.6 (via feedmail 8.3.emacs20_6 I) and Blat ver 1.8.9 In-reply-to: (message from Cameron Weeks on Sun, 09 Sep 2001 12:13:00 GMT) Subject: Re: In getopt.c wht is place declared as const References: <3B99AB68 DOT 19D91FCB AT dtcs DOT de> Reply-To: djgpp AT delorie DOT com Errors-To: nobody AT delorie DOT com X-Mailing-List: djgpp AT delorie DOT com X-Unsubscribes-To: listserv AT delorie DOT com Precedence: bulk > From: Cameron Weeks > Newsgroups: comp.os.msdos.djgpp > Date: Sun, 09 Sep 2001 12:13:00 GMT > > Thanks for the reply, I already had a basic understanding of static > and associated storage class specifiers, and const and associated > type qualifiers. Most of my problem was specific to getopt.c. Why in > this specific code have they declared place as const. Because that's how it is treated by the compiler, even if you don't declare it as such. The C language standard says that a constant string cannot be changed at run time, so many environments place it into read-only storage (so that it could be shared between different invocations of the same program, and thus conserve memory). > #define EMSG "" > static const char *place = EMSG; > > Now surely this is a slightly dangerous thing to do. If EMSG was > anything but "" then you would be writing to memory pointed to by an > un-initialised pointer, therefore risking a crash. Hmm? What uninitialized pointer? This initialization is performed at link time. The compiler creates a constant string "" and reserves 1 byte for it. The linker puts the address of that string into the variable `place'. Thereafter `place' points to where EMSG, whatever its contents are, is stored, and you cannot modify EMSG by writing into that address. The above description is true no matter what the definition of EMSG is. In other words, this statement is the one which makes `place' an initialized pointer, by pointing it to the constant string "". It doesn't dereference `place', as you seem to think.