static into structure

T

Tinku

look at this code .... i am quite confuse why i am getting error while
compiling it by gcc.

#include <stdio.h>
struct myStructure
{
int i;
static int si;
};

int main()
{
struct myStructure a;
printf("%u \n", sizeof(a));
printf("%u \n", sizeof(struct myStructure));
return 0;
}

sumit@programming$ gcc -Wall -Wextra -pedantic -ansi
static_inside_struct.c
error: expected specifier-qualifier-list before ‘static’

I dont understand why the error is coming while compiling it
cant we take static variable in structure or what ?
am i wrong some where?
if i remove static from the struct myStructure it works, but it is not
working with static variable

as we know static variable is stored somewhere else (in Data segment)
and auto variable is stored in stack (auto storage) , this is because
the error is coming ?

please teach me what exactly is being happened .
Thanks .
 
S

saurabh

look at this code .... i am quite confuse why i am getting error while
compiling it by gcc.

#include <stdio.h>
struct myStructure
{
    int i;
    static int si;

};

int main()
{
    struct myStructure a;
    printf("%u \n", sizeof(a));
    printf("%u \n", sizeof(struct myStructure));
    return 0;

}

sumit@programming$ gcc -Wall -Wextra -pedantic -ansi
static_inside_struct.c
error: expected specifier-qualifier-list before ‘static’

I dont  understand why the error is coming while compiling it
cant we take static variable in structure or what ?
am i wrong some where?
if i remove static from the struct myStructure it works, but it is not
working with static variable

as we know static variable is stored somewhere else (in Data segment)
and auto variable is stored in stack (auto storage) , this is because
the error is coming ?

please teach me what exactly is being happened .
Thanks .

I don't know If this is a proper plce to ask but still ,in addition to
what OP has asked,
I tried same programm with C++(g++) and it compiles with no problems.
So is it because 'static' means different things in C and C++ ?
 
S

saurabh

Static doesn't have any sense when applied to a struct member. Either you
apply static to the whole struct, or you don't at all.- Hide quoted text -

- Show quoted text -
Thats what the point is.
If a static member is allowed in a C++ struct,why doesnt't it have any
sense in a C struct?
 
S

saurabh

In

saurabh wrote:



C and C++ are different languages. To give you another example, C++
allows function definitions within structs. C doesn't.

C is under no obligation to codify C++ constructs.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
"Usenet is a strange place" - dmr 29 July 1999
Sig line vacant - apply within

Richard,

I am sorry If I sounded like doing a comparison of C and C++ here.
I understand that languages are tools and every tool has a place where
it fits.
My intention was not to compare features of C++ and C,what I mean to
say was,
"As having a static member variable in a c++ class means that it is
common to all
objects of that class,I assumed something like, A static member inside
a struct will
be common to all instances of that struct."
But it seems It is a wrong assumption.Just to clear my understanding
of C semantics I would
like to know why is this not possible?
 
M

Michael Schumacher

Pietro said:
Static doesn't have any sense when applied to a struct member.

That's true.
Either you
apply static to the whole struct, or you don't at all.

But what's the point in applying "static" to a type definition?
Aren't user-defined types automatically tied to the compilation
unit they're defined in? I think they are, actually.


mike
 
N

Nick Keighley

don't quote .sigs (the bit after the "-- ")

I am sorry If I sounded like doing a comparison of C and C++ here.

yoe are :)
I understand that languages are tools and every tool has a place where
it fits.

I'm not sure about this. I own a wooden butter knife.
My intention was not to compare features of C++ and C,what I mean to
say was, "As having a static member variable in a c++ class means that it is
common to all objects of that class,I assumed something like, A static
member inside a struct will be common to all instances of that struct."
But it seems It is a wrong assumption.Just to clear my understanding
of C semantics I would like to know why is this not possible?

This is a bit of an "ask Dennis Richie" question (he designed the C
language). But we can speculate a bit. A C struct is simply a way of
defining a type which is a collection of other simpler types. Although
you can assign one instance to another they have little or no other
group semantics. So you pass a struct by value to a function and
return one by value you cannot compare structs. C structs are not
quite first-class objects. This is consistent with C's close-to-the-
machine philosophy and what-you-see-is-what-you-get. C statements
usually compile into a small number of machine instructions there is
little going on beneath the surface. In fact struct assignment could
be said to getting very close to bending this principle. I suppose no
one thought of having data that was "global" to all structs of the
same type.

When Bjarne Stroustrup invented C++ he started with C then stretched
the semantics of various constructs, particularly the humble struct,
to thinks he wanted to do. he wanted embedded methods (functions) and
virtual methods and in the end static data. C saw no need to extend
the C language by adding these things. An although I programmed in
both C (quite a lot) and C++ (much less) I've never missed struct
static data from C.

Assuming just because C++ can do something therefore C can do it was
always a bad idea and much more of a bad idea now that C is a large
mature language.
 
A

arnuld

Static doesn't have any sense when applied to a struct member. Either
you apply static to the whole struct, or you don't at all.

H&S 5, section 4.3:

static:

may appear on declarations of functions and variables.
On function definitions, it is used only to specify that the function
name is not exported to the linker. On function declarations, it
indicates that the declared function will be defined - with storage
class /static/ - later in the file. On Data Declarations, it always
signifies a defining declaration that is not exported to the linker.
Variables declared with this storage class have static extent (as opposed
to local extent, signified by /auto/ )



So, a member of a struct can not be treated as a variable according to
this definition of static. or do I need to say that static applies only
to independent variables and in OP's case it ain't so ?
 
A

arnuld

..SNIP...
Read what you said.

"what I mean to say was: As having a static member variable in a c+
+ class means that it is common to all objects of that class,I assumed
something like, A static member inside a struct will be common to all
instances of that struct."

"But it seems It is a wrong assumption.Just to clear my
understanding of C semantics I would like to know why is this not
possible?"


I don't think its possible to use a word like /instance/ in C
language. Anyone ?
 
B

Ben Bacarisse

Please don't quote sigs blocks. Snip them along with anything else
that you don't want to comment on.
I am sorry If I sounded like doing a comparison of C and C++ here.
I understand that languages are tools and every tool has a place
where it fits.

My intention was not to compare features of C++ and C,what I mean to
say was, "As having a static member variable in a c++ class means
that it is common to all objects of that class,I assumed something
like, A static member inside a struct will be common to all
instances of that struct."

But it seems It is a wrong assumption.Just to clear my understanding
of C semantics I would like to know why is this not possible?

[I've reformatted the above.]

"Why" questions can have lots of answers: sociological, historical,
technical. The most useful ones here are usually technical. There is
no technical reason for C not to have the construct you are talking
about, but what would it mean? Essentially, it would be a static
object with a qualified name. I.e.:

struct vector {
static int projection;
double c1, c2, c3;
} v;

Is very similar to:

struct vector {
double c1, c2, c3;
} v;

static int vector_projection;

so there would be little to be gained. Even when accessing the member
through a pointer, the type rules of C mean you can always tell, just
be looking at the pointer types, exactly which variable is begin
referenced. In C++, this is not true anymore and the mechanism is
therefore more interesting.

What is more, the static member would be less accessible than the
static variable unless other mechanisms were added to C. In my first
example, "projection" is only accessible through an object of type
struct vector whereas vector_projection is accessible even when no
objects of type struct vector exist.
 
S

Seebs

I am sorry If I sounded like doing a comparison of C and C++ here.

No one's complaining about a hypothetical comparison, merely pointing
out that the languages are different.
"As having a static member variable in a c++ class means that it is
common to all
objects of that class,I assumed something like, A static member inside
a struct will
be common to all instances of that struct."
But it seems It is a wrong assumption.Just to clear my understanding
of C semantics I would
like to know why is this not possible?

Because it's not possible.

I mean, seriously. That's it. No one designed such a feature for C, so
there's no such feature, so it's not possible. It could have been, just
as we could have added a rule that declaring a struct member with the word
"coffee" in front of it caused the compiler to serve you a cup of high-quality
espresso.

But in the language which actually exists, there's no such thing.

If you want to get to the more theoretical question of why there wouldn't
be, it's because C is a lower-level language, and the type has no existence
separate from the objects, so there's noplace to put the hypothetical static
member; the things declared in a struct actually occur in each struct object,
in order, and there's no storage space shared among all structs of the same
type. In C++, the type really has independent existence, so it makes sense
to have an object associated with the type to hold the "shared" values.

I will go so far as to grant that, given the huge amount of overloading
that the word "static" normally takes, this one makes as much sense as any
of them.

-s
 
J

James Kuyper

No, we cannot. A "struct-declaration" (6.7.2.1p1), the part of the
grammar that describes members of structures, differs from the more
general "declaration" (6.7p1) in that it cannot include a storage class
specifier.

....
I tried same programm with C++(g++) and it compiles with no problems.
So is it because 'static' means different things in C and C++ ?

Yes, of course; they are different languages, and some of the biggest
differences are associated with the declaration of structure types (or
classes, as they are called in C++). The above declaration is
well-defined in C++, with a meaning that myStructure::si is the name of
a static int object, whose storage location is completely independent of
the location of any actual myStructure object; a simple 'si' may be used
to refer to that object in any member function. Since neither the ::
operator nor member functions are part of C, that would be completely
meaningless in C.

Also, keep in mind that static means many different things just in C
alone, never mind comparing the two language: it can indicate internal
linkage, or static storage duration, or (when it appears inside the
leading dimension of the declaration of an array parameter in C99) a
minimum requirement on the length of the corresponding array argument.
 
J

James Kuyper

saurabh wrote:
....
Richard,

I am sorry If I sounded like doing a comparison of C and C++ here.

You sounded like you were doing that, because you were doing it. You're
asking why a C++ features is not supported by C; that is, in principle,
no different from asking why a given Fortran feature is not supported by
C. They are different languages, and forgetting that fact can only lead
to trouble.
I understand that languages are tools and every tool has a place where
it fits.
My intention was not to compare features of C++ and C,what I mean to
say was,
"As having a static member variable in a c++ class means that it is
common to all
objects of that class,I assumed something like, A static member inside
a struct will
be common to all instances of that struct."

That's not a very good description. It could easily be read as implying
that the following code:

myStructure ms;
ms.si = 5;

was legal. The correct code would be myStructure::si = 5;
But it seems It is a wrong assumption.Just to clear my understanding
of C semantics I would
like to know why is this not possible?

It is possible, or C++ could not have done it. It is not supported by C
as a matter of choice, not because it couldn't be done. It is an
innovation introduced in C++. Unlike some other C++ innovations such as
'const' and 'inline', the committee has not (yet) chosen to move this
one back into C.

This is probably because the scope of struct member names is different
in C from the class scope that they have in C++. In C++, class scope is
meaningful only in class member functions - which C does not have. You
can access such members from outside of such functions only by the use
of the :: operator, which C does not have and does not need. If we
remove those restriction to allow a C static member variable, then it
would be indistinguishable from an ordinary static non-member variable,
which is something C already allows.
 
K

Keith Thompson

Michael Schumacher said:
Pietro Cerutti wrote: [...]
Static doesn't have any sense when applied to a struct member.

That's true.
Either you
apply static to the whole struct, or you don't at all.

But what's the point in applying "static" to a type definition?
Aren't user-defined types automatically tied to the compilation
unit they're defined in? I think they are, actually.

You don't apply "static" to a type; you apply it to an object.

For example:

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

arnuld

I have no problem using the word "instance" in C - as in, for example,
"an object is an instance of a type".

Yes, an object is an instance of type as long as the type is Class which
is an OO thingy. I use the word object for an instance of a Class. K&R2
use it for "anything which occupies memory". So I guess you use it in
sense of K&R2 by that means:

int i = 0;

i is an instance of int. I feel good with "i is a variable of type int".
 
K

Keith Thompson

arnuld said:
Yes, an object is an instance of type as long as the type is Class which
is an OO thingy. I use the word object for an instance of a Class.

C, of course, doesn't have classes.
K&R2
use it for "anything which occupies memory".

Does K&R2 actually use those exact words?
So I guess you use it in
sense of K&R2 by that means:

int i = 0;

i is an instance of int. I feel good with "i is a variable of type int".

Certainly "i is a variable of type int", or "i is an object of type
int", is correct. The issue is whether referring to i as an
"instance" of type int is also correct. I'd say it is, though I
probably wouldn't use that word.

<OT>I'm not sure that the C++ standard used the word "instance"
with any special technical meaning either. I do know that its
definition of the word "object" is very similar to C's, and is not
limited to objects of class type.</OT>
 
A

arnuld

Does K&R2 actually use those exact words?

Can't remember. I have a hard-copy and tried finding it where I read bt
was unsuccessful.
Certainly "i is a variable of type int", or "i is an object of type
int", is correct. The issue is whether referring to i as an "instance"
of type int is also correct. I'd say it is, though I probably wouldn't
use that word.

<OT>I'm not sure that the C++ standard used the word "instance" with any
special technical meaning either. I do know that its definition of the
word "object" is very similar to C's, and is not limited to objects of
class type.</OT>

So /instance/ is not a technical word here, just plain English.
 
A

arnuld

That usage has a long history. OOP is a johnny-come-lately; in any case,
where do you think they got the word "object" from in the first place?

From medical sciences ? where Doctors use to refer to patient as subject
and something else as object.

But long time ago before that object was already in English, something
like "I am the object of amusement"

const int j = 0;
Do you feel good about "j is a variable with type int"? Yet i and j
clearly have many (indeed, almost all) characteristics in common, and it
is useful to have a term that describes them both. "Object" is perfect
for that.

I wonder why did you use /const/. Anyway, I got confused because you
said /Object/ instead of /object/.
 

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
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top