Path: csiph.com!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: Ways to avoid global variables Date: Sun, 05 Apr 2020 16:12:50 -0700 Organization: None to speak of Lines: 76 Message-ID: <875zedplvx.fsf@nosuchdomain.example.com> References: Mime-Version: 1.0 Content-Type: text/plain Injection-Info: reader02.eternal-september.org; posting-host="9aca32966cb66c1a01b4dff59d5f3d44"; logging-data="19654"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18qni7PaivmRs3ginXjwSdy" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.3 (gnu/linux) Cancel-Lock: sha1:y4HHRztppI6LCjEaT3NPY0MlFYk= sha1:XKBM8eTxOnqe7RnrG7Ekj9n//JE= Xref: csiph.com comp.lang.c:151106 ram@zedat.fu-berlin.de (Stefan Ram) writes: > James Kuyper writes: >>On 4/5/20 11:07 AM, Stefan Ram wrote: >>>Pointers can be the null pointer or point to entities >>>(functions or objects), but not to variables. >>I'm curious - what do you consider the definition of "variable" to be, >>that justifies your claim that pointers can't point at them? > > A variable contains all the information that is given by the > declaration of a name and by the scope of this declaration. > This encompasses the name itself, its type, and also, for > example, whether this is an automatic variable. > > For example, in the block, > > { int x; } > > , a variable with the name "x" and the type "int" and > automatic storage duration is declared. Like most definitions of the word "variable" I've seen, this makes statements about variables or about what they do, but it doesn't tell me what a variable *is*. I think most programmers would assume that a "variable" is a kind of object. Others might say that it's some kind of abstract thing that can refer to an object. My best guess from your wording is that a "variable" is a compile-time symbol table entry. Given: int x; int *p = &x; I would say that p points to a variable -- or, since C doesn't say what a "variable" is, that p points to a named object. It's certainly true that p does not give access to the name "x". > Akin to: > > |In C, all variables must be declared before they are used, > |usually at the beginning of the function before any > |executable statements. A declaration announces the > |properties of variables; it consists of a name and a list of > |variables, such as > |int fahr, celsius; > |int lower, upper, step; > The C Programming Language, Second Edition > > When one has a pointer to an object that is bound to a name, > one cannot reach information about the name. > > When a pointer would be able to point to a variable, I would > then also expect to be able to obtain the name via the pointer. > > Via recursion, several objects can be bound to a variable that > is declared once. This shows that there is not a 1:1 correspondence > between variables and objects. > >>In my experience, by far the most common problem with global variables >>IS their excessive visibility, so I would not describe that as "merely". > > Global variables /hide/ dependencies of functions. > > For example, the behavior of > > void print( int const n ){ printf( "%d\n", n ); } > > depends on "stdout", but this is not shown in the interface. > > -- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com Working, but not speaking, for Philips Healthcare void Void(void) { Void(); } /* The recursive call of the void */