471,056 Members | 1,497 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

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

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

May 24 '06 #1
4 2376
sandeep wrote:
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++...

--
Ian Collins.
May 24 '06 #2
On Wed, 24 May 2006 02:38:38 -0700, sandeep wrote:
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".

--
Ben.
May 24 '06 #3

sandeep wrote:
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
access only selected structure member )
following code gives syntax error
struct xxxx
{
static int i; //
int j;
};

Thanks
Sandeep


May 24 '06 #4
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.

--
pete
May 24 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Tino | last post: by
6 posts views Thread by Eric Smith | last post: by
4 posts views Thread by Bill Pursell | last post: by
12 posts views Thread by Ark | last post: by
5 posts views Thread by Mahendra Kumar Kutare | last post: by
7 posts views Thread by John Koleszar | last post: by
reply views Thread by leo001 | last post: by

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.