473,511 Members | 14,134 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

typeless data [structures]

Rob
I seek to be able to store data in a static variable that has no
specifically defined type/size. Or to put it another way, I wish to
create a structure of data but without the ability to reference its
subcomponents until I cast it to another predefined type.

In my attempts of doing this (I don't know if it is even possible), I
encountered a feature of some sort that seems to be legal, but yet I
don't know what it does. It involves inserting a type followed by a
colon followed by a constant. Here's an example:

struct {

int : 0;
long : 5;
short : 2;
} s;

What does this do? Does it have anything to do with the functionality I
am after? When I attempted to cast this struct it wouldn't allow me to;
it gave the error "cannot convert from '' to <type_name>".

Nov 14 '05 #1
4 4375
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Rob wrote:
I seek to be able to store data in a static variable that has no
specifically defined type/size.
AFAIK, C has no way to permit you to do that.
Or to put it another way, I wish to
create a structure of data but without the ability to reference its
subcomponents until I cast it to another predefined type.
I can think of a couple of ways to accomplish this goal, and none of them
involve untyped data.
In my attempts of doing this (I don't know if it is even possible), I
encountered a feature of some sort that seems to be legal, but yet I
don't know what it does. It involves inserting a type followed by a
colon followed by a constant. Here's an example:

struct {

int : 0;
long : 5;
short : 2;
} s;

What does this do?
It pads bitfields.

Say, you have two bitfields that you want to separate by eight bits. You'd
define a structure something like this...

struct {
int bit_field_1 : 4;
int : 8;
int bit_field_2 : 4;
} s;

Does it have anything to do with the functionality I
am after?
No, it doesn't
When I attempted to cast this struct it wouldn't allow me to;
it gave the error "cannot convert from '' to <type_name>".


Yah, I'd expect something like that

So, to solve your problem, try hiding your data in a char array, and cast its
address to a pointer to your structure.

- --
Lew Pitcher

Master Codewright & JOAT-in-training | GPG public key available on request
Registered Linux User #112576 (http://counter.li.org/)
Slackware - Because I know what I'm doing.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFCm70gagVFX4UWr64RAtrXAKCxf/1VJaHuM5C91LoYKTXmn67L+wCeOFRo
jcjuCoGIaqdjodp5+IwqxWU=
=Ucqq
-----END PGP SIGNATURE-----
Nov 14 '05 #2
"Rob" <io*********@hotmail.com> wrote:
# I seek to be able to store data in a static variable that has no
# specifically defined type/size. Or to put it another way, I wish to
# create a structure of data but without the ability to reference its
# subcomponents until I cast it to another predefined type.

C wants to know the size of things when allocates space for variables. One
way around that is to use pointers, because the compiler knows the size of
pointers. You can use a (void*) pointer as your variable and then cast it
to a pointer of whatever struct you want.

# struct {
#
# int : 0;
# long : 5;
# short : 2;
# } s;
#
# What does this do? Does it have anything to do with the functionality I
# am after? When I attempted to cast this struct it wouldn't allow me to;
# it gave the error "cannot convert from '' to <type_name>".

It's about using integers smaller than their natural size packed into a
structure.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
You hate people.
But I love gatherings. Isn't it ironic.
Nov 14 '05 #3
Groovy hepcat Rob was jivin' on 30 May 2005 17:26:03 -0700 in
comp.lang.c.
typeless data [structures]'s a cool scene! Dig it!
I seek to be able to store data in a static variable that has no
specifically defined type/size. Or to put it another way, I wish to
create a structure of data but without the ability to reference its
subcomponents until I cast it to another predefined type.
Sounds like what you want to do is hide data behind an incomplete
pointer type. It's a common practice.
Define the struct within a separate translation unit containing
functions to handle (assign, return & manipulate) data of this type.
Define an incomplete pointer type to refer to this data. Let these
functions store the data in a structure, and keep track of the data
with the incomplete pointers.
In my attempts of doing this (I don't know if it is even possible), I
encountered a feature of some sort that seems to be legal, but yet I
don't know what it does. It involves inserting a type followed by a
colon followed by a constant. Here's an example:

struct {
int : 0;
long : 5;
short : 2;
} s;

What does this do? Does it have anything to do with the functionality I
am after?
No. This creates bitfields, sort of like tiny integers.
When I attempted to cast this struct it wouldn't allow me to;
it gave the error "cannot convert from '' to <type_name>".


Of course. You can't cast a struct.

--

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"?
Nov 14 '05 #4


Lew Pitcher wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Rob wrote:
I seek to be able to store data in a static variable that has no
specifically defined type/size.
AFAIK, C has no way to permit you to do that.
Or to put it another way, I wish to
create a structure of data but without the ability to reference its
subcomponents until I cast it to another predefined type.


I can think of a couple of ways to accomplish this goal, and none of them
involve untyped data.
In my attempts of doing this (I don't know if it is even possible), I
encountered a feature of some sort that seems to be legal, but yet I
don't know what it does. It involves inserting a type followed by a
colon followed by a constant. Here's an example:

struct {

int : 0;
long : 5;
short : 2;
} s;

What does this do?


It pads bitfields.


I agree that it is used to pad in the case of bitfields.
but seeing to above struct I guess they are trying to achive the
padding for the 7 bits, But this can be done with out using the
int also, Does some thing is hidden ?, for which OP has tried
the int with the allocation of 0 bit i.e int : 0;

I suppose above struct can be re-modified in the below format
struct {
long : 5;
short : 2;
}s;

as this too allocate the 7 bit as compared to the above,
Here I mean not the size (I know that both the struct will
give) as 1 byte as the size.

Regards
Ranjeet
Say, you have two bitfields that you want to separate by eight bits. You'd
define a structure something like this...

struct {
int bit_field_1 : 4;
int : 8;
int bit_field_2 : 4;
} s;
Does it have anything to do with the functionality I
am after?


No, it doesn't
When I attempted to cast this struct it wouldn't allow me to;
it gave the error "cannot convert from '' to <type_name>".


Yah, I'd expect something like that

So, to solve your problem, try hiding your data in a char array, and cast its
address to a pointer to your structure.

- --
Lew Pitcher

Master Codewright & JOAT-in-training | GPG public key available on request
Registered Linux User #112576 (http://counter.li.org/)
Slackware - Because I know what I'm doing.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFCm70gagVFX4UWr64RAtrXAKCxf/1VJaHuM5C91LoYKTXmn67L+wCeOFRo
jcjuCoGIaqdjodp5+IwqxWU=
=Ucqq
-----END PGP SIGNATURE-----


Nov 14 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
478
by: hzy_104 | last post by:
Please recommend book on data structures for searching(c++)?
5
2248
by: el_roachmeister | last post by:
For being a good web programmer, is a course on data structures important? It seems php already has built-in functions for what they teach in a data structures course. On the other hand all...
4
3834
by: Thomas Paul Diffenbach | last post by:
Can anyone point me to an open source library of /statically allocated/ data structures? I'm writing some code that would benefit from trees, preferably self balancing, but on an embedded system...
10
4749
by: Bart Goeman | last post by:
Hi, I have a question about how to put redundant information in data structures, initialized at compile time. This is often necessary for performance reasons and can't be done at run time (data...
13
5210
by: Leszek Taratuta | last post by:
Hello, I have several drop-down lists on my ASP.NET page. I need to keep data sources of these lists in Session State. What would be the most effective method to serialize this kind of data...
3
2292
by: osp | last post by:
hi to every one.... i just started out with c++ and i think i am doing well.i use Robert Laffore to study. which book should i use for data structures ? please help. thank you with regards ...
11
3731
by: efrat | last post by:
Hello, I'm planning to use Python in order to teach a DSA (data structures and algorithms) course in an academic institute. If you could help out with the following questions, I'd sure...
29
6315
by: Mik0b0 | last post by:
Hallo to everyone. This fall I am going to start data structures as a part of C language course. The problem is I could not find any satisfying tutorial about structures in C. There are plenty of...
4
2038
by: jehugaleahsa | last post by:
Hello: When developing data structures for C#, there is an obvious performance hit when utilizing primitive types. For instance, a recent hash table implementation I wrote works exceedingly fast...
0
7137
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7349
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
7417
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...
1
7074
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7506
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
4734
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3219
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
445
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.