Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #22614

Re: C scope and struct definitions

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: C scope and struct definitions
Date 2012-06-24 11:00 -0700
Organization None to speak of
Message-ID <lnpq8o7fp7.fsf@nuthaus.mib.org> (permalink)
References <mYGdnXsGt41dNnnSnZ2dnUVZ_u-dnZ2d@bestweb.net> <js5l0m$64l$1@dont-email.me> <js5oki$9m3$1@speranza.aioe.org> <lny5nd78em.fsf@nuthaus.mib.org> <js698k$7rm$1@speranza.aioe.org>

Show all headers | View raw


pacman@kosh.dhis.org (Alan Curry) writes:
> In article <lny5nd78em.fsf@nuthaus.mib.org>,
> Keith Thompson  <kst-u@mib.org> wrote:
>>pacman@kosh.dhis.org (Alan Curry) writes:
>>
>>> Anyway, putting type names and variable names into a single category
>>> and calling them "identifiers" is a false generalization. It works
>>> at the technical level for the compiler implementor I guess, but
>>> besides that what's the reason for lumping them together and
>>> insisting that they behave the same?  If you can't explain it
>>> without the word "identifier" then there is no reason, it's just a
>>> thought process that's being misled by a bit of escaped
>>> compiler-writer's jargon.
>>
>>I'd say it's a perfectly valid generalization. They *are* both
>>identifiers, and they follow the same scope rules.
>
> They "are" both identifiers because the word "identifier" has been
> defined that way. This is a circular argument. It doesn't prove that
> they belong together in a class, just that they have been forced into
> one and an awkward name has been slapped on it.

There's nothing arbitrary about the meaning of the word
"identifier". It's a syntactic construct, consisting of ... well,
you can read the grammar as well as I can.

You find it awkward that the same scope rules apply to identifers
that name objects and identifiers that name types.  I find it simple
and consistent, and I think most other people do as well.

I think I understand your point of view; you see types as a
fundamentally different kind of entity than objects, and you
don't think it makes sense for types to have anything other than
file scope.

My point of view is that the current scoping rules for type names
is sensible, that absolutely nothing prevents you from defining
all your types at file scope, and the restriction you suggest has
no great benefit, would make the language marginally more complex,
and would break existing code.

I'm not asking you to share my point of view, but can you understand
it?

>>You advocate treating type names as a special case with different scope
>>rules than other identifiers. That's a valid preference, but not one
>>that I share -- and not one that's consistent with C as it's currently
>>defined.
>
> More like no scope rules at all. Just a global table of types. (Global
> within a single run of the compiler). Any time a new struct tag is
> seen, just add it to the table and leave it there until the compiler
> exits. A type has an existence inherently outside of any function
> because types are more fundamental than functions.

So you deny the possibility that it might make sense to have a type
that's used only within one function.  that's fine, but if you don't
mind I'd rather you didn't try to impose that model on every C
programmer in the world.

>>> The same could be said for a function that's only called by one other
>>> function. Should we have nested functions then?
>>
>>Many languages do have nested functions. What tends to make them
>>complicated is the need to define rules for accessing objects defined
>>in an outer function. I wouldn't strongly object to adding nested
>>functions to C (and gcc supports them as an extension), but I don't
>>think they're all that important; we've gotten along without them
>>for a very long time.
>
> That complication (closures) is a good reason to want nested functions. It
> can be emulated without help from the compiler, but not easily. What's the
> corresponding reason for wanting struct definitions inside a function?

The point is that you can define a struct type that's only used within a
function, and that type is not made unnecessarily visible outside the
function.

>>
>>[...]
>>
>>> Variable scopes make sense because the object named by the variable doesn't
>>> exist until the function is called. A type isn't an object.
>>
>>Are you conflating scope and lifetime?
>>
>
> Are you seriously saying they aren't (or shouldn't be) closely tied together?

Yes.

Ok, they're tied together in some ways, but it's a loose tie. An
object declared inside a function has block scope, but its lifetime
can be either the nearest enclosing block *or* the entire execution
of the program, depending on whether it has the "static" keyword.
An object allocated by malloc() has allocated lifetime, but *no
scope* (since it doesn't have a name).

In the C language as it's currently defined, you can do exactly what
you want: define all types at file scope.  The change you propose
would not help you do that -- but it would break existing code.

Out of curiosity, are you talking about how you wish the language
had been defined in the first place, or are you proposing a change
to a future version of the language?

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
    Will write code for food.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

C scope and struct definitions Kenneth Brody <kenbrody@spamcop.net> - 2012-06-22 13:30 -0400
  Re: C scope and struct definitions Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-06-22 14:02 -0400
    Re: C scope and struct definitions pacman@kosh.dhis.org (Alan Curry) - 2012-06-22 22:40 +0000
      Re: C scope and struct definitions Ben Pfaff <blp@cs.stanford.edu> - 2012-06-22 15:47 -0700
      Re: C scope and struct definitions Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-06-22 20:32 -0400
        Re: C scope and struct definitions pacman@kosh.dhis.org (Alan Curry) - 2012-06-23 22:06 +0000
          Re: C scope and struct definitions Keith Thompson <kst-u@mib.org> - 2012-06-23 16:18 -0700
            Re: C scope and struct definitions James Kuyper <jameskuyper@verizon.net> - 2012-06-25 10:19 -0400
              Re: C scope and struct definitions Keith Thompson <kst-u@mib.org> - 2012-06-25 11:51 -0700
          Re: C scope and struct definitions Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-06-23 19:54 -0400
            Re: C scope and struct definitions pacman@kosh.dhis.org (Alan Curry) - 2012-06-24 00:56 +0000
              Re: C scope and struct definitions Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-06-23 21:48 -0400
                Re: C scope and struct definitions pacman@kosh.dhis.org (Alan Curry) - 2012-06-24 06:03 +0000
                Re: C scope and struct definitions nick_keighley_nospam@hotmail.com - 2012-06-25 00:05 -0700
              Re: C scope and struct definitions Keith Thompson <kst-u@mib.org> - 2012-06-23 19:25 -0700
                Re: C scope and struct definitions pacman@kosh.dhis.org (Alan Curry) - 2012-06-24 05:40 +0000
                Re: C scope and struct definitions Jens Gustedt <jens.gustedt@loria.fr> - 2012-06-24 10:47 +0200
                Re: C scope and struct definitions Keith Thompson <kst-u@mib.org> - 2012-06-24 11:00 -0700
                Re: C scope and struct definitions pacman@kosh.dhis.org (Alan Curry) - 2012-06-25 03:53 +0000
                Re: C scope and struct definitions Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-06-25 01:14 -0400
                Re: C scope and struct definitions Keith Thompson <kst-u@mib.org> - 2012-06-24 22:38 -0700
                Re: C scope and struct definitions Kenneth Brody <kenbrody@spamcop.net> - 2012-06-25 11:03 -0400
                Re: C scope and struct definitions pacman@kosh.dhis.org (Alan Curry) - 2012-06-26 20:08 +0000
                Re: C scope and struct definitions Jens Gustedt <jens.gustedt@loria.fr> - 2012-06-26 23:21 +0200
                Re: C scope and struct definitions pacman@kosh.dhis.org (Alan Curry) - 2012-06-26 21:47 +0000
                Re: C scope and struct definitions Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-06-26 18:05 -0400
                Re: C scope and struct definitions Keith Thompson <kst-u@mib.org> - 2012-06-26 16:45 -0700
                Re: C scope and struct definitions Jens Gustedt <jens.gustedt@loria.fr> - 2012-06-27 08:26 +0200
                Re: C scope and struct definitions Keith Thompson <kst-u@mib.org> - 2012-06-27 00:01 -0700
                Re: C scope and struct definitions James Kuyper <jameskuyper@verizon.net> - 2012-06-27 06:57 -0400
                Re: C scope and struct definitions James Kuyper <jameskuyper@verizon.net> - 2012-06-27 15:00 -0400
                Re: C scope and struct definitions Keith Thompson <kst-u@mib.org> - 2012-06-27 14:48 -0700
                Re: C scope and struct definitions Tim Rentsch <txr@alumni.caltech.edu> - 2012-06-27 16:14 -0700
                Re: C scope and struct definitions Tim Rentsch <txr@alumni.caltech.edu> - 2012-06-29 09:36 -0700
                Re: C scope and struct definitions Tim Rentsch <txr@alumni.caltech.edu> - 2012-06-27 16:27 -0700
                Re: C scope and struct definitions Keith Thompson <kst-u@mib.org> - 2012-06-27 12:03 -0700
                Re: C scope and struct definitions Keith Thompson <kst-u@mib.org> - 2012-06-27 12:11 -0700
                Re: C scope and struct definitions Tim Rentsch <txr@alumni.caltech.edu> - 2012-06-27 15:53 -0700
                Re: C scope and struct definitions Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-06-26 17:59 -0400
                Re: C scope and struct definitions pacman@kosh.dhis.org (Alan Curry) - 2012-06-26 22:18 +0000
                Re: C scope and struct definitions Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-06-26 18:33 -0400
                Re: C scope and struct definitions Keith Thompson <kst-u@mib.org> - 2012-06-26 17:22 -0700
                Re: C scope and struct definitions Keith Thompson <kst-u@mib.org> - 2012-06-27 00:03 -0700
                Re: C scope and struct definitions Kenneth Brody <kenbrody@spamcop.net> - 2012-06-27 14:13 -0400
                Re: C scope and struct definitions Keith Thompson <kst-u@mib.org> - 2012-06-27 12:15 -0700
                Re: C scope and struct definitions Tim Rentsch <txr@alumni.caltech.edu> - 2012-06-29 09:43 -0700
                Re: C scope and struct definitions Keith Thompson <kst-u@mib.org> - 2012-06-29 11:59 -0700
                Re: C scope and struct definitions Tim Rentsch <txr@alumni.caltech.edu> - 2012-07-01 18:32 -0700
                Re: C scope and struct definitions pacman@kosh.dhis.org (Alan Curry) - 2012-06-27 20:59 +0000
                Re: C scope and struct definitions Kenneth Brody <kenbrody@spamcop.net> - 2012-06-28 16:42 -0400
            Re: C scope and struct definitions James Kuyper <jameskuyper@verizon.net> - 2012-06-23 20:57 -0400
              Re: C scope and struct definitions Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-06-23 22:11 -0400
                Re: C scope and struct definitions James Kuyper <jameskuyper@verizon.net> - 2012-06-23 22:51 -0400
                Re: C scope and struct definitions Nick Bowler <nbowler@draconx.ca> - 2012-06-25 17:31 +0000
                Re: C scope and struct definitions James Kuyper <jameskuyper@verizon.net> - 2012-06-25 14:46 -0400
                Re: C scope and struct definitions Tim Rentsch <txr@alumni.caltech.edu> - 2012-06-25 14:43 -0700
                Re: C scope and struct definitions Jens Gustedt <jens.gustedt@loria.fr> - 2012-06-26 13:44 +0200
                Re: C scope and struct definitions Keith Thompson <kst-u@mib.org> - 2012-06-23 20:07 -0700
              Re: C scope and struct definitions Jens Gustedt <jens.gustedt@loria.fr> - 2012-06-24 10:56 +0200
                Re: C scope and struct definitions James Kuyper <jameskuyper@verizon.net> - 2012-06-24 08:29 -0400
              Re: C scope and struct definitions Tim Rentsch <txr@alumni.caltech.edu> - 2012-06-24 10:26 -0700
          Re: C scope and struct definitions Kenneth Brody <kenbrody@spamcop.net> - 2012-06-25 10:50 -0400
      Re: C scope and struct definitions Tim Rentsch <txr@alumni.caltech.edu> - 2012-06-24 09:23 -0700
        Re: C scope and struct definitions Kenneth Brody <kenbrody@spamcop.net> - 2012-06-25 11:17 -0400
          Re: C scope and struct definitions Tim Rentsch <txr@alumni.caltech.edu> - 2012-06-25 14:22 -0700
  Re: C scope and struct definitions Kenneth Brody <kenbrody@spamcop.net> - 2012-06-25 11:11 -0400

csiph-web