Steve Valliere
Steve Valliere

Reputation: 1207

C struct with file scope

Question: Are the tags on structures that are declared as static at file scope private to the file in the same way that the actual struct variable being declared will be? Or are the structure tags common across files?

This question came from a problem I am having with the MSVS2019 debugger showing me the wrong values and struct member names in some files. (You can read about the bug here, if you're interested: Visual Studio 2019 Debugger Issue) The files were all cloned from an original and all have something like this:

static struct MyPrivateData_s
{
   char *szData[64];
} myData;
static int myCount;

The actual members of the struct vary from file-to-file and the values are quite different for each.

I know that variables that are declared static, but above any functions in a source file have "file scope" -- are global to that file only. I'm pretty certain the VS debugging system has a problem, but it made me wonder about how (or even if) the static storage class affects the structure tag.

If the tag is treated like the name of the struct and other variables, then it is private to the file. However, if the tag is treated differently, it may be triggering the VS bug. Remember, I am asking about only the structure tag, not the name of the actual structure variable. It would also be good to know how names from a typedef are affected by 'static' as well.

I have been trying to find the answer in all of the various C references I own and can find on line, but I (a) don't know the right question to ask and/or (b) am not understanding (or recognizing?) the answer when I find it. Hopefully a C guru can help me out.

FWIW, I think that anything following static at file scope should be restricted to the file where it is found. In fact, only below the point it is found in the file. That is how I have always coded and I have never encountered an issue before this debugger thing made me wonder about this. Thanks!

I think this may answer my question: Limit Struct Scope even though it does not explicitly use the term "tag". Instead, it appears to be calling the tag a "type", which, it becomes when prefixed by 'struct'. So static has no effect, but the fact that the tag appears in a C file and NOT in a common header means that the tag is private to the file. Sorry to have been a bother.

Upvotes: 2

Views: 1119

Answers (2)

Eric Postpischil
Eric Postpischil

Reputation: 222923

static does not affect structure tags or scope.

There is no scope beyond file scope (C 2018 6.2.1 1). Identifiers in different scopes can refer to the same object via linkage (6.2.2 1). If the different scopes are in different translation units, the identifiers can refer to the same object if they have external linkage.

Structure tags do not have linkage (6.2.2 6: “The following identifiers have no linkage: an identifier declared to be anything other than an object or a function;…”) Therefore, structure tags in different translation units refer to different entities.

This means that structure types in different translation units are different types. One would then wonder how the same structure can be used in different translation units. Per 6.2.7 1, structure types in different translation units may be compatible. For structures, compatibility largely requires identical definitions, with some allowances such as possibly omitting a tag.

The presence of static in a declaration (but not inside [ and ]) affects linkage (6.2.2 3) and storage duration (6.2.4 3) for objects and functions.

This answers this question:

Question: Are the tags on structures that are declared as static at file scope private to the file in the same way that the actual struct variable being declared will be? Or are the structure tags common across files?

static affects neither structure tags nor scope. It affects whether object and function identifiers can be related across translation units due to linkage, not scope.

typedef names are aliases.

It would also be good to know how names from a typedef are affected by 'static' as well.

C 2018 6.7.8 3 says “… A typedef declaration does not introduce a new type, only a synonym for the type so specified…” As above, static affects only objects and functions; it does not affect types or type names.

Upvotes: 3

C. R. Ward
C. R. Ward

Reputation: 93

Try this instead:

struct _myprivdata_s_ {
    char *szData[64];
};
typedef struct _myprivdata_s_ MyPrivateData_s;
static MyPrivateData_s myData;
static int myCount;

Upvotes: 0

Related Questions