473,396 Members | 2,102 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Why connot declare a static member of STRUCT or UNION?

Any comments? thanks.

Jim

Oct 23 '07 #1
27 5456
ar******@gmail.com wrote:
Any comments? thanks.
Please put your question in the body of your post and don't SHOUT.

C isn't C++.

--
Ian Collins.
Oct 23 '07 #2
On Mon, 22 Oct 2007 18:14:08 -0700, ar******@gmail.com wrote in
comp.lang.c:
Any comments? thanks.
Because there is no such thing as STRUCT or UNION in C.

--
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
Oct 23 '07 #3
In article <11**********************@k35g2000prh.googlegroups .com>,
<ar******@gmail.comwrote:
>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 ???
--
"History is a pile of debris" -- Laurie Anderson
Oct 23 '07 #4
ar******@gmail.com wrote:
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.
--
C FAQ: http://c-faq.com/
Oct 23 '07 #5
ar******@gmail.com wrote:
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.

Oct 23 '07 #6
ar******@gmail.com wrote:
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++.
Oct 23 '07 #7
"James Kuyper Jr." <ja*********@verizon.netwrites:
ar******@gmail.com wrote:
>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++.
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?
Oct 23 '07 #8
Richard wrote:
"James Kuyper Jr." <ja*********@verizon.netwrites:
>ar******@gmail.com wrote:
>>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++.

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'.
Oct 23 '07 #9
James Kuyper Jr. wrote:
Richard wrote:
>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)?
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>

Oct 23 '07 #10

santosh wrote:
James Kuyper Jr. 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?
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.
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?
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.

Oct 23 '07 #11
"James Kuyper Jr." <ja*********@verizon.netwrites:
Richard wrote:
>"James Kuyper Jr." <ja*********@verizon.netwrites:
>>ar******@gmail.com wrote:
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++.

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)?
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.

Oct 24 '07 #12
James Kuyper <ja*********@verizon.netwrites:
Richard wrote:
>"James Kuyper Jr." <ja*********@verizon.netwrites:
...
>>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.
...
>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.
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.
Oct 25 '07 #13
Richard wrote:
James Kuyper <ja*********@verizon.netwrites:
>Richard wrote:
>>"James Kuyper Jr." <ja*********@verizon.netwrites:
...
>>>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.
...
>>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.

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.
Oct 25 '07 #14
In article <5i************@news.individual.netRichard <rg****@gmail.comwrites:
....
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"?
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Oct 25 '07 #15
In article <5i************@news.individual.net>,
Richard <rg****@gmail.comwrote:
>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
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Oct 25 '07 #16
In article <lr************@news.individual.net>,
Richard <rg****@gmail.comwrote:
....
(someone else)
>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.
It's another one of those "feigned ignorance" things that are so
popular here. If you want to maintain that everything that is known is
covered by your book, then you must maintain ignorance of things outside
the book.

I.e., you are not allowed to discriminate between the various flavors of
non-existence that a thing might have.

Oct 25 '07 #17
Richard Tobin wrote:

[regarding 'static' members of structs and unions, illegal in C]
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?
<OT>
That's how it works in C++. It is often used for instance counters.
</OT>
Oct 25 '07 #18
Peter Pichler said:
Richard Tobin wrote:

[regarding 'static' members of structs and unions, illegal in C]
>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?

<OT>
That's how it works in C++. It is often used for instance counters.
</OT>
Many years ago, I was helping a friend (we'll call him "Carl", since that
wasn't his name) to design a C++ program. He was a university student at
the time. As it happens, he was in the process of designing a "student"
class, and I was acting in the capacity of sounding-board. We had recently
discussed data hiding, and the importance of ensuring that a class "knows"
only information that is relevant to it.

Carl observed that it would be useful to have a static member in the
"student" class that kept track of the number of students. I suggested
that this might not be a good idea, but he remained unconvinced.

"Carl", said I, "what do you do for a living?"

"Well, I don't, yet. I'm a student; you know that already", says Carl.

"Where are you a student, Carl?"

Carl instantly spotted the maieutic approach (he was used to it by now),
and sighed. "<fooUniversity", he replied.

"How many students are there at <fooUniversity, Carl? What is the exact
number?"

"Dashed if I know", he bowdlerised.

"I rest my case", said I.

Students don't know how many students there are. Dogs don't know how many
dogs there are. Cars don't know how many cars there are. Static class
instance trackers are a design flaw.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Oct 25 '07 #19
"Dik T. Winter" <Di********@cwi.nla écrit dans le message de news:
Jq********@cwi.nl...
In article <5i************@news.individual.netRichard <rg****@gmail.com>
writes:
...
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"?
So what ?

consider:

struct s { char buf[0]; };
struct s ar[10];

what is ``sizeof ar'' ?

--
Chqrlie.
Oct 26 '07 #20
Dik T. Winter wrote:
In article <5i************@news.individual.netRichard <rg****@gmail.comwrites:
....
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?
In the case of C++, somewhere other than in the struct.
And consider:
struct s {static int member;};
struct s ar[10];
what is "sizeof ar"?
The same as

struct s {};
struct s ar[10];

--
Ian Collins.
Oct 26 '07 #21
Richard Heathfield wrote:
Students don't know how many students there are. Dogs don't know how many
dogs there are. Cars don't know how many cars there are. Static class
instance trackers are a design flaw.
Excellent point!

I never said it was a good design. Only that it is often used that way.

No, I personally have never used it in my programs. Never found the
need. Thanks to RH for an explanation why.
Oct 26 '07 #22
In article <gP******************************@bt.com>,
Richard Heathfield <rj*@see.sig.invalidwrote:
>Students don't know how many students there are. Dogs don't know how many
dogs there are. Cars don't know how many cars there are. Static class
instance trackers are a design flaw.
If you regard the "static" value as a property of a student, then you
are right. But if you consider a property of the class of students,
it makes reasonable sense. The class of students may know how many
instances it contains. It's then just a syntactic infelicity that you
write it as if it were a property of the instance.

-- Richard

--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Oct 26 '07 #23
ja*********@verizon.net writes:
Richard wrote:
>James Kuyper <ja*********@verizon.netwrites:
...
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.

a) I don't see any currently defined meaning for static that is
identified as the starting point for your extension.
I dont play word games. It is clear enough what I meant reading back. As
I said this is nothing to do with C++ or what the specs say now.

A static member of a struct has a blindingly obvious "simple"
meaning. Once you have understood that then you can explain why my
"simple" meaning can not ever work and why I am being a dumb arse to
suggest it is that simple.

I see you snipped my basic description of what I considered it *could*
mean.

I am more than a tad confused as to your apparent difficulty with what
is a merely hypothetical discussion.
Oct 26 '07 #24
ri*****@cogsci.ed.ac.uk (Richard Tobin) writes:
In article <5i************@news.individual.net>,
Richard <rg****@gmail.comwrote:
>>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
Are we off on a tangent now? it's easy to do. I am referring to a static
structure member. e.g a field in a struct where the field is a
static. Note for those tempted to drop in late here : read rest before
hopping in saying "that is not what the spec says" please :-;
Oct 26 '07 #25
Richard wrote:
ja*********@verizon.net writes:
>Richard wrote:
>>James Kuyper <ja*********@verizon.netwrites:
...
>>>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.
a) I don't see any currently defined meaning for static that is
identified as the starting point for your extension.

I dont play word games. It is clear enough what I meant reading back. As
I agree: this is clear:
All structures share a common field value across all instances of that
structure.
What isn't clear is why you consider this a "blindingly obvious" meaning
for a static member declaration.
I said this is nothing to do with C++ or what the specs say now.

A static member of a struct has a blindingly obvious "simple"
meaning.
I can't see those two statements as consistent. If the new meaning has
nothing to do with any previously defined meaning for 'static', why in
the world do you consider it a "blindingly obvious" meaning?
Once you have understood that then you can explain why my
"simple" meaning can not ever work and why I am being a dumb arse to
suggest it is that simple.
I never said it couldn't work.

I said that it violates the C object model and unnecessarily adds a new
meaning to a keyword that is already overworked. I also pointed out that
it must either create a gratuitous incompatibility with C++, or require
borrowing some fairly complicated features of C++, or be completely
useless. I've also pointed out that the "gratuitous incompatibility"
option (accessing the static member through the member selection
operator '.') would create a feature of only marginal usefulness, since
it adds little or no value compared to the currently available option of
declaring a static variable outside of the structure.

It certainly could work.
Oct 26 '07 #26
On Oct 25, 11:03 am, rich...@cogsci.ed.ac.uk (Richard Tobin) wrote:
In article <5if1v4-rf6....@news.individual.net>,

Richard <rgr...@gmail.comwrote:
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?
That's the C++ usage (modifiable, all instances see the current
value), and that's how Richard's using the term.

To me, the right answer to the OP is "if you want C++, use C++."

Oct 26 '07 #27
>"Dik T. Winter" <Di********@cwi.nla écrit dans le message de news:
>Jq********@cwi.nl...
>>... consider:
struct s {static int member;};
struct s ar[10];
what is "sizeof ar"?
In article <47***********************@news.free.fr>
"Charlie Gordon" <ne**@chqrlie.orgwrote:
>consider:
struct s { char buf[0]; };
This is not legal in C (either C89 or C99) -- array sizes must be
positive, not merely nonnegative -- so this:
struct s ar[10];
what is ``sizeof ar'' ?
does not arise. In C99, you can attempt:

struct s { char buf[]; };

which would make s have a single flexible array member, but a
flexible array member, if one is present, must be last member in
a structure, *and* (this is the key item) must come after some
other member(s), so that sizeof(struct s) is nonzero. Furthermore,
arrays may not have as their element-type a structure type that
contains a flexible array member, so once again the question "what
is sizeof ar" does not arise.

In article <5o************@mid.individual.net>,
Ian Collins <ia******@hotmail.comwrote:
>The same as

struct s {};
This, too, is not legal in C.

(C++ has a funny little special case rule for otherwise-empty
struct types. C simply forbids them entirely.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Oct 27 '07 #28

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Doug Eleveld | last post by:
Hi Everyone, I have been playing aroung with 'object-oriented' C for a while now and I have come up with an interesting way to simulate C++ inheritance and non-member functions in C in a 100%...
1
by: Michael B Allen | last post by:
Consider the following structure with a union: struct shape { int type; union { struct circle c; struct square s; struct polydoodle p; } u; };
15
by: Geoff Cox | last post by:
Hello, Can I separately declare and initialize a string array? How and where would I do it in the code below? It was created using Visual C++ 2005 Express Beta 2 ... In C# I would have ...
11
by: ziman137 | last post by:
Hi all, I have a question here. What is the rationale behind ISO C++ for Static Member Definition? * ISO C++ forbids in-class definition/initialization of non-constant static member...
5
by: sharat | last post by:
hi all can any body tell how to declare the structure pointer within a class. i have written the following program but gettin error. #include<iostream> using namespace std;
3
by: Hallvard B Furuseth | last post by:
to find the required alignment of a struct, I've used #include <stddef.h> struct Align_helper { char dummy; struct S align; }; enum { S_alignment = offsetof(struct Align_helper, align) };
14
by: deepak | last post by:
Hi Experts, I'm getting this compilation error while trying to access a member in structure. at what time we will get this error message? Thanks, Deepak
23
by: copx | last post by:
I don't know what to think of the following.. (from the dietlibc FAQ) Q: I see lots of uninitialized variables, like "static int foo;". What gives? A: "static" global variables are initialized...
7
by: Peter Olcott | last post by:
Why can a union have a member with a copy constructor?
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.