Why C doen't allow if member of the structure is static?

J

Joona I Palaste

Ganesh Kundapur said:
Hi all,
struct test {
static int x;
}X;
main()
{
X.x = 100;
}
When i compile this code, i get fallowing errors
----------------------------------------------------
x.c:2: parse error before "static"
x.c:2: warning: no semicolon at end of struct or union
x.c:3: warning: data definition has no type or storage class
x.c: In function `main':
x.c:7: request for member `x' in something not a structure or union
-------------------------------------------------------
If i remove static, it works fine. Can someone explain me what happerns
if i declare the structure member as static.

First tell us what it would *MEAN* for a structure member to be static.

--
/-- Joona Palaste ([email protected]) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"Hasta la Vista, Abie!"
- Bart Simpson
 
G

Ganesh Kundapur

Hi all,
struct test {
static int x;
}X;

main()
{
X.x = 100;
}

When i compile this code, i get fallowing errors
----------------------------------------------------
x.c:2: parse error before "static"
x.c:2: warning: no semicolon at end of struct or union
x.c:3: warning: data definition has no type or storage class
x.c: In function `main':
x.c:7: request for member `x' in something not a structure or union
 
N

NumLockOff

static scope modifier in C applies to global scoped identifiers and narrows
their scope to the function (if applied to a variable) and file scope if
applied to a function.

Your construct is a C++ construct where the static in C++ is considered to
be Class Scoped and not Class instance scoped.
 
N

Noah Roberts

NumLockOff said:
static scope modifier in C applies to global scoped identifiers and narrows
their scope to the function (if applied to a variable) and file scope if
applied to a function.

Huh?

A static modifier limits a global's scope to the file it is declaired in
(this applies to any symbol afaik), a static modifier in a function
creates a variable with a persistant value limited in scope to that
function.

NR
 
R

Ramaraj

Hi all ,

I hope the following ionformation is helpful for those who were raised
this q'tion.

The strcutre member cann't be shared across the instances of strcture.
since static member is one which is shared (along with scope). so we cann't
declare the static variable

eg:
struct x {
static int i;
........
} X1, X2;

"i" cann't be shared in X1, X2 instance it is the
constraint of c language struct but this problem is over comed in c++ .

with Regard
Ramaraj
 
D

Dan Pop

In said:
struct test {
static int x;
}X;

main()
{
X.x = 100;
}

When i compile this code, i get fallowing errors
----------------------------------------------------
x.c:2: parse error before "static"
x.c:2: warning: no semicolon at end of struct or union
x.c:3: warning: data definition has no type or storage class
x.c: In function `main':
x.c:7: request for member `x' in something not a structure or union

Exactly what you have already observed: you get compiler diagnostics.

Dan
 
I

Irrwahn Grausewitz

To whom are you responding? Hopefully not to Dan Pop... ;-)
maybe C does not allow static members in a struct. C++ does.
^^^^^
delete this

Regards
 
R

Ravi

Hi all,
struct test {
static int x;
}X;

main()
{
X.x = 100;
}

When i compile this code, i get fallowing errors

Next time mention the compiler you used.

I get this error
on TC++:

Error 2: Storage class 'static' is not allowed here

(:cool:
 
P

Peter Shaggy Haywood

Groovy hepcat NumLockOff was jivin' on Thu, 9 Oct 2003 23:40:12 -0700
in comp.lang.c.
Re: Why C doen't allow if member of the structure is static?'s a cool
scene! Dig it!
static scope modifier in C applies to global scoped identifiers and narrows
their scope to the function (if applied to a variable) and file scope if
applied to a function.

Nonsense! Firstly, static is a storage class specifier, not a "scope
modifier". There ain't so such animal.
Secondly, what the static storage class specifier does is dependant
on where the identifier is defined. Those defined at file scope
(including functions) are rendered inaccessible (by name) to other
translation units. Those defined at block scope are given static
duration - ie., they survive the life of the program. At no time does
the static storage class specifier alter the scope of an identifier.

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
 

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

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top