In what scope is a struct member identifier put?

前端 未结 4 1762
南方客
南方客 2021-01-18 10:25

The C spec says

There are four kinds of scopes: function, file, block, and function prototype.

Now if I do the following outside any

相关标签:
4条回答
  • 2021-01-18 11:09

    I think the scope it refers to in this case is A. You refer to the member by SomeA.x, where SomeA is of type A.

    0 讨论(0)
  • 2021-01-18 11:20

    As the standard says, it's in a separate name space created for the struct type. This rule was added in C89, I think. Once upon a time, all member names shared a single name space.

    Maybe the compiler writer was being pedantic; a name space is not the same as a scope. A struct definition does not introduce a scope as it doesn't hold variables; it holds members.

    0 讨论(0)
  • 2021-01-18 11:24

    Scope and name space are two entirely different concepts and should not be confused.

    An instance of a struct or union has scope, but the individual member names do not; the concept of scope simply doesn't apply to them.

    Argh, I was thinking in terms of extent, not scope.

    0 讨论(0)
  • 2021-01-18 11:26

    x is in the scope created by struct A, but A is in global scope. This is what the "spec" calls file-scope but the name is misleading because all files that you include share that scope.

    0 讨论(0)
提交回复
热议问题