Why connot declare a static member of STRUCT or UNION?

W

Walter Roberson

Any comments? thanks.

What would you expect it to mean? That, in the case of a struct,
that the rest of the struct would lose its values when the struct
went out of scope, but the static member would not? That, in
the case of a union, that... that what? That if you had last written
to it via the static member that the value would be remembered
but that it wouldn't be remembered if you had written to the
union via one of the non-static members ???
 
R

Road Tang

Any comments? thanks.

Jim

In Standard C, it can't.
because C doesn't support it. C isn't a language that supports
OO programming favor.

You can use C++, and post in comp.lang.c++ .

-Road.
 
S

santosh

Any comments? thanks.

Note that C is case sensitive. STRUCT and UNION are not defined by the C
Standard, you probably meant struct and union.

As for your question, a struct or union is an aggregate type. That means
all it's member objects have the same scope. Though the static
qualifier can apply to the whole structure, it's application to one of
it's members makes very little sense; and I see little point in it too.
 
J

James Kuyper Jr.

Any comments? thanks.

The keyword 'static' has many meanings in C, none of which make sense
inside a struct. C++ gave that keyword an additional meaning, which
applies only in that context, but this newsgroup is for discussing C,
not C++.
 
R

Richard

James Kuyper Jr. said:
The keyword 'static' has many meanings in C, none of which make sense
inside a struct. C++ gave that keyword an additional meaning, which
applies only in that context, but this newsgroup is for discussing C,
not C++.

It would make perfect sense to see static in a struct as far as I can
see. What is so nonsensical about it? Or is your point not that it
wouldn't make sense, but that it's simply not available?
 
J

James Kuyper Jr.

Richard said:
It would make perfect sense to see static in a struct as far as I can
see. What is so nonsensical about it? Or is your point not that it
wouldn't make sense, but that it's simply not available?

The standard defines (at least) three different meanings for the keyword
static, depending upon whether it's at file scope, block scope, or
function prototype scope. A single struct type, once defined, could be
used to declare an object in any of those scopes, so the possible
meaning of the 'static' keyword when applied to a member would have to
change with the scope.

File scope: what would it mean for a structure object to have external
linkage, but a member of that object to have internal linkage (or vice
versa)?

Block scope: What would it mean for a structure object to be
automatically or dynamically allocated, and to have a member that was
statically allocated (or vice versa)?

Function prototype scope: the only meaning currently provided for static
in this scope is that it requires that the leading dimension of an array
passed as a specific argument to the function to be at least as long as
the length specified for the corresponding parameter. I don't see how
that could be applied to struct members.

I'm not saying that answers couldn't be invented for these questions;
I'm merely saying that any answer would be a new invention, not a simple
extension to the currently defined meanings of 'static'.
 
R

Richard

James Kuyper Jr. said:
The standard defines (at least) three different meanings for the
keyword static, depending upon whether it's at file scope, block
scope, or function prototype scope. A single struct type, once
defined, could be used to declare an object in any of those scopes, so
the possible meaning of the 'static' keyword when applied to a member
would have to change with the scope.

File scope: what would it mean for a structure object to have external
linkage, but a member of that object to have internal linkage (or vice
versa)?

I'm not sure I follow you. The "meaning" seems clear enough to me. All
structures share a common field value across all instances of that
structure.
Block scope: What would it mean for a structure object to be
automatically or dynamically allocated, and to have a member that was
statically allocated (or vice versa)?

static is static. The structure references the static value.
Function prototype scope: the only meaning currently provided for
static in this scope is that it requires that the leading dimension of
an array passed as a specific argument to the function to be at least
as long as the length specified for the corresponding parameter. I
don't see how that could be applied to struct members.

I'm not sure I know what you are talking about here. I fail to see how
this has anything to do with a static structure field.
I'm not saying that answers couldn't be invented for these questions;
I'm merely saying that any answer would be a new invention, not a
simple extension to the currently defined meanings of 'static'.

I'm not sure anything you have said means that it doesn't "make sense"
for "static" to mean something reasonable inside a structure. The fact
that it isn't part of C is neither here nor there to the conversation
about a hypothetical thing anyway.
 
S

santosh

James said:
Richard wrote:

The standard defines (at least) three different meanings for the
keyword static, depending upon whether it's at file scope, block
scope, or function prototype scope. A single struct type, once
defined, could be used to declare an object in any of those scopes, so
the possible meaning of the 'static' keyword when applied to a member
would have to change with the scope.

File scope: what would it mean for a structure object to have external
linkage, but a member of that object to have internal linkage (or vice
versa)?

Presumably that the static member is invisible to other modules?
Block scope: What would it mean for a structure object to be
automatically or dynamically allocated, and to have a member that was
statically allocated (or vice versa)?

Presumably that the static member retains it's previous value?

<snip>
 
J

jameskuyper

santosh said:
James Kuyper Jr. wrote: ....

Presumably that the static member is invisible to other modules?

Reasonable, but how would you declare it in the other modules?
Certainly something like this could be defined, but it's not a simple
extension of the existing C language.
Presumably that the static member retains it's previous value?

That's a concept that could certainly be developed, but unless the
structure itself is also declared static, on most implementations I'm
familiar with, that would require that the static member be stored
somewhere other than the other members of the struct, which is
inconsistent with C's object model. C++ has an object model that has
been modified to accomodate this idea. Large portions of the C
standard would need to be reviewed to make sure they still make sense
with the revised object model.

I hope you're not suggesting that both of these answers be used? I
think that would cause a lot of headaches. I was expecting an answer
that would choose one or the other meaning, and impose the same
meaning, regardless of what scope the object was defined in.
 
J

Jack Klein

Presumably that the static member is invisible to other modules?

Horse apples. Nothing in a structure is accessible from other
translation units (other than as an array of unsigned chars) unless
the struct definition is visible. The identical struct definition,
with the same types in the same order having the same names.
Presumably that the static member retains it's previous value?

All members in a struct object are "static" throughout its lifetime.
As long as the object exists, each member retains its last stored
value until such time as it is assigned another value.

But once an automatic instance of the struct goes out of scope, or a
dynamically allocated instance is free()'d, the object ceases to exist
and there is no way it can retain a previous value.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
 
J

James Kuyper

Richard said:
....
I'm not sure I follow you. The "meaning" seems clear enough to me. All
structures share a common field value across all instances of that
structure.

That's obvious only by reference to C++; it's not in any sense a
uniquely obvious extension of any of the current meanings of 'static' in C.

....
I'm not sure anything you have said means that it doesn't "make sense"
for "static" to mean something reasonable inside a structure. The fact
that it isn't part of C is neither here nor there to the conversation
about a hypothetical thing anyway.

My point, which I should have stated more plainly, is that the OP should
not have simply asked why the static keyword couldn't be used; if he
wanted to be able to use it, he should have specified what meaning he
wanted it to have. Simply saying "the same as in C++" would have been
inadequate, but it would be much better than providing no hint as to the
desired meaning.


Had he put the request in those terms, I would have pointed out that in
C++, static members are accessible from outside a class only by use of
the scope resolution operator '::', and are accessible without the scope
resolution operator only within member functions of the class or its
sub-classes. Since C has neither the scope resolution operator, nor
member functions, in order for static members to be useful they could
work "the same as in C++" only if one or both of those things is also
added to C.

You could make static members accessible through the member access
operator '.', but that would create a gratuitous incompatibility between
C and C++, something that both the C and the C++ have policies against.
 
R

Richard

James Kuyper said:
That's obvious only by reference to C++; it's not in any sense a
uniquely obvious extension of any of the current meanings of 'static'
in C.

We would have to beg to differ. It appears fairly clear to me what would
be meant by a static structure field if such existed in C.
 
J

James Kuyper

Richard said:
We would have to beg to differ. It appears fairly clear to me what would
be meant by a static structure field if such existed in C.

Can you point to one of the existing meanings for 'static' in C, and
explain how this is a unique natural extension of that meaning? It seems
quite different from any of the three that I listed.
 
R

Richard

James Kuyper said:
Can you point to one of the existing meanings for 'static' in C, and
explain how this is a unique natural extension of that meaning? It
seems quite different from any of the three that I listed.

I already did. It's above.

I also pointed out that I am not talking about any "existing
documentation of static", just that it does not take a degree in CS to
come to a relatively easy deduction of what a static field in a struct
*might* mean in some alternative universe.

Possibly you have a much more detailed view of what static might mean -
but in my mind its fairly straightforward and clear in the, possibly,
simplistic ways in which I have used it and/or understood it.
 
S

santosh

Richard said:
I already did. It's above.

I also pointed out that I am not talking about any "existing
documentation of static", just that it does not take a degree in CS to
come to a relatively easy deduction of what a static field in a struct
*might* mean in some alternative universe.

Possibly you have a much more detailed view of what static might mean
- but in my mind its fairly straightforward and clear in the,
possibly, simplistic ways in which I have used it and/or understood
it.

Specifically we might consider the cases of static as applicable for
file scope and block scope objects.

To me only the case of block scope makes much sense.

file.h:

struct foo {
int x;
static int y;
};

file.c:

#include "file.h"

/* within some function/block */
struct foo bar;

In this case bar.y should retain it's value as any other block scope
static object.

I'm sure the experts can point to numerous loopholes and untenable
assumptions. :)
 
D

Dik T. Winter

> I'm not sure I follow you. The "meaning" seems clear enough to me. All
> structures share a common field value across all instances of that
> structure.

And where and when is that common field allocated?
And consider:
struct s {static int member;};
struct s ar[10];
what is "sizeof ar"?
 
R

Richard Tobin

Richard said:
I'm not sure I follow you. The "meaning" seems clear enough to me. All
structures share a common field value across all instances of that
structure.

As someone who doesn't use C++, my first thought is that this would be
more like a strict "const" than "static". Or do you mean that it
would be modifiable and all instances see the current value, effectively
a new kind of global variable?

-- Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,438
Messages
2,571,699
Members
48,796
Latest member
Greg L.
Top