typeless data [structures]

R

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>".
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
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-----
 
S

SM Ryan

# 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.
 
P

Peter Shaggy Haywood

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"?
 
R

ranjeet.gupta

Lew said:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


AFAIK, C has no way to permit you to do that.


I can think of a couple of ways to accomplish this goal, and none of them
involve untyped data.


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
 

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,164
Messages
2,570,898
Members
47,439
Latest member
shasuze

Latest Threads

Top