www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2001/09/08/01:31:02

From: Leif Leonhardy <leif AT dtcs DOT de>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: In getopt.c wht is place declared as const
Date: Sat, 08 Sep 2001 07:23:52 +0200
Organization: delta t Computerservice
Lines: 99
Message-ID: <3B99AB68.19D91FCB@dtcs.de>
References: <lvR6$iAbUXm7EwSj AT cweeks DOT demon DOT co DOT uk>
NNTP-Posting-Host: pd9e0e1c2.dip0.t-ipconnect.de
Mime-Version: 1.0
X-Trace: news.online.de 999926552 25208 217.224.225.194 (8 Sep 2001 05:22:32 GMT)
X-Complaints-To: abuse AT online DOT de
NNTP-Posting-Date: 8 Sep 2001 05:22:32 GMT
X-Mailer: Mozilla 4.01 [de] (Win95; I)
X-Priority: 3 (Normal)
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

> I am a newbie to C and DJGPP, I think both are wonderful, and DJGPP is currently
that's implied by your question ;-)

> there is a variable called "place" that is declared as
> 
> "static const char *place"
> 
> Why have the const part of this statement, my particular curiosity was raised
> because later on in getopt.c place is unconst-ed, it seems needless, just don't
> bother declaring it as const in the first place.
If it was 'needless' (i.e., wrong) the compiler would complain...

	const some_type *pointer 

declares 'pointer' to point to some constant data (i.e., you MUST NOT
use
'pointer' to modify data it points to), the pointer itself is NOT
constant
(it may point to different data at different times).

> It is also declared as static under
> the getopt() function so it seems pretty safe from inadvertent alteration.
As its declaration is even local (in scope) to getopt(), it cannot be
modified
by other functions by referencing it BY ITS NAME (they just can't see
it).

You see, if 'static' should have any meaning in this case, it has to be
a different one.

The designers of C (and also C++) preferred briefness and a "small"
language,
so they even reduced the number of keywords (reserved identifiers) by
reusing
them in different contexts. 
(There actually have been discussions about the number of keywords a
language defines
 and the time and space it takes to parse/compile them. Anyway, there
are (still) 
 keywords in C nobody uses and very few people at all know, e.g.
'auto'.)

The meaning of storage class 'static' OUTSIDE of a function is that the
variable
declared - to be exact, its name - is local to the (C-)FILE it is
declared in,
so you can declare variables with the same name (but possibly different
types)
in different modules and link them together without name clashes.
'static' does
not provide any protection from outside-modification unless you make
sure that
you don't pass its ADDRESS to other modules.

The meaning of 'static' WITHIN A FUNCTION is that this variable is
allocated
in the "global" data area, which is preserved over function calls, in
contrast
to declaring it (implicitly) 'auto' (on the stack) or 'register'. If you
initialize such variable in its declaration ('static int foo=1;'), it
will
be initialized ONLY ONCE when the function is called for the first time
(this can
be a pitfall):

	void say_hello()
	{ static int i=0;

	  for(i=0;i<10;i++)
	    printf("Hello, world\n");
        }

	main()
	{ int n;

 	  for(n=0;n<1000;n++)
	    say_hello();

	  return 0;
	}

How often will the world be greated? The answer is ten times, not
10,000.

In C++, 'static' has even further meanings (class variables as opposed
to
object variables).

If you want to learn about the meaning of sometimes mystic declarations,
pick the program 'cdecl' (which is fairly old, but I think there is a
version on some djgpp mirror site?); it takes a declaration in C syntax
and explains it in natural language. There should be a C++ version,
also.

return (volatile void(*)(const int[],char*[],double(*)()))bye;

Have fun,

Leif

- Raw text -


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