why can't we use static type as structure member?

S

sandeep

Hi
why we cannot have static as a structure member?
& also is there any way to achive data hiding in C at this level( i.e.
access only selected structure member )
following code gives syntax error
struct xxxx
{
static int i; //
int j;
};

Thanks
Sandeep
 
I

Ian Collins

sandeep said:
Hi
why we cannot have static as a structure member?

Because they aren't part of standard C.
& also is there any way to achive data hiding in C at this level( i.e.
access only selected structure member )

Use C++...
 
B

Ben Bacarisse

Hi
why we cannot have static as a structure member?

In part because the answer to your second question is "no". Without
"restricted access", a static structure member is just a global variable
with a name related to the structure type. You can get the same effect
by writing:

int struct_xxxx_i = 42;

(and you can get some data hiding if you make it static.)
& also is there any way to achive data hiding in C at this level( i.e.
access only selected structure member ) following code gives syntax
error
struct xxxx
{
static int i; //
int j;
};

There is no direct language support for hiding structure members in C, but
you can do a lot of data hiding like this:

In widget.h:

struct widget {
/* "public" members of widgets here */
};

/* interface to widgets: */

struct widget *make_widget(/* args */);
void fiddle_widget(struct widget *wp /* other args */);

In widget.c:

#include "widget.h"

struct real_widget {
struct widget public;
/* "private" members here */
};

static int widget_count = 0;

struct widget *make_widget(/* args */)
{
struct real_widget *rwp = malloc(sizeof *rwp);
if (rwp) {
++widget_count;
/* other code */
return &rwp->public; /* or: return (struct widget *)rwp; */
}
else return NULL;
}

void fiddle_widget(struct widget *wp /*other args */)
{
struct real_widget *rwp = (struct real_widget *)wp;
/* whatever ... */
}

Many C programmers would find this rather over the top: just put all the
(non static) structure members in one structure and tell people in a
comment what they can and cannot touch.

On the other hand, putting static data in a file like this is often done in
C programs.

Of course, if all the data is "private" then you can just have one
structure and move its definition into widget.c. Outside of that file you
pass around struct widget pointers with no idea what they have in them --
the type becomes "opaque".
 
S

sandy

sandeep said:
Hi
why we cannot have static as a structure member?
& also is there any way to achive data hiding in C at this level( i.e.

Well, What I think is that the the memory that is allocated for a
static member is always on th data segment and never on th stack, as
the value will get lost when the specific function exits.

So, this provision is not provided by C to club together two types of
variables. The problem will come when you declare a variable in a
function of this struct type. The value has to be allocted from the
stack for that variable. And then the problem....will come into
picture.


Cheers,
Sandeepksinha
 
P

pete

sandy wrote:
Well, What I think is that the the memory that is allocated for a
static member is always on th data segment and never on th stack, as
the value will get lost when the specific function exits.

The Stack and data segments may have something
to do with the way that your implementation implements C,
but the answer to the question,
"why we cannot have static as a structure member?"
in the context of this newsgroup
should be in terms of the rules of C.
In short, "The Stack and data segments"
are off topic for this newsgroup.
 

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,183
Messages
2,570,967
Members
47,517
Latest member
Andres38A1

Latest Threads

Top