473,401 Members | 2,146 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,401 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 2534
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Tino | last post by:
From Stroustrup( TC++PL, Appendix C.13 ) I thought that this was the way (and the only way perhaps) to have a common static member for all classes generated from a template. Though compiling this...
7
by: alphatan | last post by:
Is there relative source or document for this purpose? I've searched the index of "Mastering Regular Expression", but cannot get the useful information for C. Thanks in advanced. -- Learning...
6
by: Eric Smith | last post by:
Is a structure containing an incomplete array as its last element (per paragraph 2 of section 6.7.2.1 of ISO/IEC 9899:1999 (E)) itself an incomplete type? That appears to be indicated by paragraph...
4
by: Bill Pursell | last post by:
I've been thinking of doing things like the following: $ cat foo.h struct foo{ int a; void (*init)(struct foo *, int); }; void new_foo(struct foo *self);
12
by: Ark | last post by:
Hello NG, I arrange data in structs like { members... uint16_t crc; more members, maybe... } Then I need to save them, up to and including crc, in non-volatile memory or a file, as the case...
3
by: ranjeetasharma81 | last post by:
Hi all, I have a big C-cod, in which there are lots of dynamic memory allocation used. I want to replace dynamic memroy allocation by static arrays. The following are the problems that i am...
5
by: Mahendra Kumar Kutare | last post by:
I am trying to implement a webserver with boss-worker model thread pool implementation - I have a header declaration threadpool.h as - typedef struct threadpool_work { void (*routine) ();...
27
by: arkmancn | last post by:
Any comments? thanks. Jim
7
by: John Koleszar | last post by:
Hi all, I'm porting some code that provides compile-time assertions from one compiler to another and ran across what I believe to be compliant code that won't compile using the new compiler. Not...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.