Creating array of data types

M

Madhur

Hi All,

I would like you help me in creating an array of data types.

I am interested in look at the the data type which looks like this


Array a[10]={int,float,char,int*..............................},

so that a[0] should return me int and a[1] should return me
float..which helps me in runtime type casting.

I do not know how to create such an array. If i create so what would
be the data type of "Array".
This is basically a variable to "data type" conversion.

Or is there any better way to do this.

Looking keenly for the response..

Regards,
Madhur
 
M

Mark Bluemel

Madhur said:
Hi All,

I would like you help me in creating an array of data types.

I don't think I can do that...
I am interested in look at the the data type which looks like this


Array a[10]={int,float,char,int*..............................},

What the **** does that mean?
so that a[0] should return me int and a[1] should return me
float..which helps me in runtime type casting.

You could possibly do something with unions, but the idea of arrays is
that elements are essentially equivalent. If you know that a[0] is an
int, a[1] a float, etc then they should simply be individual variables,
I'd have thought.
I do not know how to create such an array. If i create so what would
be the data type of "Array".
This is basically a variable to "data type" conversion.

Or is there any better way to do this.

I think you should tell us what you are trying to achieve in broader
terms - your original requirement - rather than telling us how you are
trying to do it.
 
M

Madhur

Thanks for the info...

Array as referred above is list hundreds of user defined data types
and it is impossible for me to take care of it each time unless i have
an explicit hash maintained to it which i can not entertain right
now.. therefore I was looking for array of data types..which seems to
be impossible...

Yes Enumeration is the only way to go..but is there any how i can link
an index to the enum data type...say enum looks like this

typedef union enum1
{
type0,
type1,
type2,
type3
..
..
..
type n
}ENUM1;

so if get an index say 2 can i extract type2 without using type2
explicity..
in better way..is there somehow ican mention ENUM(2) and which should
get me type2????




Madhur said:
I would like you help me in creating an array of data types.

I don't think I can do that...
I am interested in look at the the data type which looks like this
Array a[10]={int,float,char,int*..............................},

What the **** does that mean?
so that a[0] should return me int and a[1] should return me
float..which helps me in runtime type casting.

You could possibly do something with unions, but the idea of arrays is
that elements are essentially equivalent. If you know that a[0] is an
int, a[1] a float, etc then they should simply be individual variables,
I'd have thought.


I do not know how to create such an array. If i create so what would
be the data type of "Array".
This is basically a variable to "data type" conversion.
Or is there any better way to do this.

I think you should tell us what you are trying to achieve in broader
terms - your original requirement - rather than telling us how you are
trying to do it.
 
R

Richard Heathfield

Mark Bluemel said:

What the **** does that mean?

In a declaration context, it means that does is a pointer to pointer to
pointer to pointer to an object of type the.

In an expression context, it could mean that the is to be multiplied by
the value pointed to by the value pointed to by the value pointed to by
does, or it could simply mean the value pointed to by the value pointed
to by the value pointed to by the value pointed to by does.

This article was brought to you by courtesy of the Coincidentally Right
Justified Usenet Articles Association. Happily, the coincidence extends
even to the very paragraph that describes it.
 
M

Mark Bluemel

Madhur said:
Thanks for the info...

Please don't top post.
Array as referred above is list hundreds of user defined data types
and it is impossible for me to take care of it each time unless i have
an explicit hash maintained to it which i can not entertain right
now.. therefore I was looking for array of data types..which seems to
be impossible...

Yes Enumeration is the only way to go..but is there any how i can link
an index to the enum data type...say enum looks like this

typedef union enum1
{
type0,
type1,
type2,
type3
.
.
.
type n
}ENUM1;

so if get an index say 2 can i extract type2 without using type2
explicity..
in better way..is there somehow ican mention ENUM(2) and which should
get me type2????

I think you need an enumeration of types:-

enum TYPE {
INT,
LONG,
FLOAT,
DOUBLE,
...
};

A union of data values :-
union values {
int ival;
long lval;
float fval;
double dval;
..
};

And an array of structures :-

struct data_item {
enum TYPE type;
union values value;
};

struct data_item my_data[100];

Then you can use a switch to get the data for a particular data_item.
You could even have a really ugly macro -

#define ENUM(n) (my_data[n].type == INT)?my_data[n].value.ival: \
(my_data[n].type == LONG)?my_data[n].value.lval: \
...

Untested code etc...
 
M

Madhur

Madhur said:
Thanks for the info...

Please don't top post.




Array as referred above is list hundreds of user defined data types
and it is impossible for me to take care of it each time unless i have
an explicit hash maintained to it which i can not entertain right
now.. therefore I was looking for array of data types..which seems to
be impossible...
Yes Enumeration is the only way to go..but is there any how i can link
an index to the enum data type...say enum looks like this
typedef union enum1
{
type0,
type1,
type2,
type3
.
.
.
type n
}ENUM1;
so if get an index say 2 can i extract type2 without using type2
explicity..
in better way..is there somehow ican mention ENUM(2) and which should
get me type2????

I think you need an enumeration of types:-

enum TYPE {
INT,
LONG,
FLOAT,
DOUBLE,
...

};

A union of data values :-
union values {
int ival;
long lval;
float fval;
double dval;
..

};

And an array of structures :-

struct data_item {
enum TYPE type;
union values value;

};

struct data_item my_data[100];

Then you can use a switch to get the data for a particular data_item.
You could even have a really ugly macro -

#define ENUM(n) (my_data[n].type == INT)?my_data[n].value.ival: \
(my_data[n].type == LONG)?my_data[n].value.lval: \
...

Untested code etc...

But what is the data type of the variable which stores the result thus
returned...
which brings again back to the question...
 
M

Mark Bluemel

Madhur said:
Madhur said:
Thanks for the info...

Please don't top post.





On Jul 24, 1:34 pm, Mark Bluemel <[email protected]> wrote:
I think you should tell us what you are trying to achieve in broader
terms - your original requirement - rather than telling us how you are
trying to do it.
Array as referred above is list hundreds of user defined data types
and it is impossible for me to take care of it each time unless i have
an explicit hash maintained to it which i can not entertain right
now.. therefore I was looking for array of data types..which seems to
be impossible...
Yes Enumeration is the only way to go..but is there any how i can link
an index to the enum data type...say enum looks like this
typedef union enum1
{
type0,
type1,
type2,
type3
.
.
.
type n
}ENUM1;
so if get an index say 2 can i extract type2 without using type2
explicity..
in better way..is there somehow ican mention ENUM(2) and which should
get me type2????

I think you need an enumeration of types:-

enum TYPE {
INT,
LONG,
FLOAT,
DOUBLE,
...

};

A union of data values :-
union values {
int ival;
long lval;
float fval;
double dval;
..

};

And an array of structures :-

struct data_item {
enum TYPE type;
union values value;

};

struct data_item my_data[100];

Then you can use a switch to get the data for a particular data_item.
You could even have a really ugly macro -

#define ENUM(n) (my_data[n].type == INT)?my_data[n].value.ival: \
(my_data[n].type == LONG)?my_data[n].value.lval: \
...

Untested code etc...


But what is the data type of the variable which stores the result thus
returned...

God only knows. As my "Untested code" comment suggests, I only threw the
code together, I didn't analyse it. The macro approach may well not work
worth beans - indeed it may not even compile.
which brings again back to the question...

If you have to do what I think you are trying to do, I think you will
need some big switch statements to process the data appropriately to its
type. You cannot expect to handle different data types identically.
 
K

Keith Thompson

Madhur said:
I would like you help me in creating an array of data types.

I am interested in look at the the data type which looks like this


Array a[10]={int,float,char,int*..............................},

so that a[0] should return me int and a[1] should return me
float..which helps me in runtime type casting.

I do not know how to create such an array. If i create so what would
be the data type of "Array".
This is basically a variable to "data type" conversion.

Or is there any better way to do this.

C has no way to represent types at execution time. The only solution
is to keep track of it yourself. This is likely to be error-prone; if
a typo causes your program to treat something as an int when it should
be treated as a float, the compiler isn't going to tell you about the
error.

You might be able to reduce the risk of errors by writing a program to
generate some of your C code for you automatically.

Or you might be better off using a language other than C.

<OT>
C++ has something called RTTI (run-time type information). I've just
told you everything I know about it. I don't know whether it will
actually solve your problem. For more information, consult a C++ book
or tutorial, or Google it. Any questions should be directed to
comp.lang.c++, *not* here.
</OT>

<OT>
Other languages might also provide helpful features; you might try
comp.lang.misc or comp.programming.
</OT>
 
M

Madhur

Madhur said:
I would like you help me in creating an array of data types.
I am interested in look at the the data type which looks like this
Array a[10]={int,float,char,int*..............................},
so that a[0] should return me int and a[1] should return me
float..which helps me in runtime type casting.
I do not know how to create such an array. If i create so what would
be the data type of "Array".
This is basically a variable to "data type" conversion.
Or is there any better way to do this.

C has no way to represent types at execution time. The only solution
is to keep track of it yourself. This is likely to be error-prone; if
a typo causes your program to treat something as an int when it should
be treated as a float, the compiler isn't going to tell you about the
error.

You might be able to reduce the risk of errors by writing a program to
generate some of your C code for you automatically.

Or you might be better off using a language other than C.

<OT>
C++ has something called RTTI (run-time type information). I've just
told you everything I know about it. I don't know whether it will
actually solve your problem. For more information, consult a C++ book
or tutorial, or Google it. Any questions should be directed to
comp.lang.c++, *not* here.
</OT>

<OT>
Other languages might also provide helpful features; you might try
comp.lang.misc or comp.programming.
</OT>

--
Keith Thompson (The_Other_Keith) (e-mail address removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

RTTI functionality can not be thought of in C, as C doesn't care about
the objects being created.
Hence the concepts of typeid does not work.
It would have been good to implement RTTI functionality in C too..
My problem has no solution in C but has in C++..
 
S

santosh

Don't quote signatures unless you're commenting on them.
RTTI functionality can not be thought of in C, as C doesn't care about
the objects being created.
Hence the concepts of typeid does not work.
It would have been good to implement RTTI functionality in C too..
My problem has no solution in C but has in C++..

Err, and your question?
 
R

Richard

santosh said:
Don't quote signatures unless you're commenting on them.

So don't requote them.

Big hint : people tend to do as you do. You don't win any clc points by
taking over from Falconer as the biggest boorish fool to net nanny in
this group.
Err, and your question?

I have one. Why did you bother to reply?
 
J

John Bode

Hi All,

I would like you help me in creating an array of data types.

I am interested in look at the the data type which looks like this

Array a[10]={int,float,char,int*..............................},

so that a[0] should return me int and a[1] should return me
float..which helps me in runtime type casting.

I do not know how to create such an array. If i create so what would
be the data type of "Array".
This is basically a variable to "data type" conversion.

Or is there any better way to do this.

Looking keenly for the response..

It would help to know what the larger problem you're trying to solve
is. Why do you need such a structure? You mention runtime type
casting; what are you casting, and why are you casting it?

It's not possible to build an array of types in C; what you can do is
build an array of records, each of which describes a type:

enum TypeKey {UCHAR = 0, TYPE_FIRST = UCHAR, CHAR, USHORT, SHORT,
UINT, INT, ULONG, LONG, FLOAT, DOUBLE, ..., TYPE_LAST = ...};

struct typeRecord {
char *name;
size_t size;
...
};

typeRecord typeArr[TYPE_LAST - TYPE_FIRST];

typeArr[UCHAR].name = "unsigned char";
typeArr[UCHAR].size = sizeof (unsigned char);
typeArr[UCHAR]... /* any additional properties */

typeArr[CHAR].name = "char";
typeArr[CHAR].size = sizeof (char);
....

etc., etc., etc.

Again, without knowing what the actual problem you're trying to solve
is, we can't really be much help.
 
M

Mark Bluemel

Default said:
Is it not blindingly obvious by now that Richard is a troll? Why
respond to this sort of thing?

It's a little more complex than that. I struggled for a while with
whether or not to killfile Richard, and checked in Google Groups to see
whether he actually posted useful stuff at all. The evidence there
seemed to indicate that he did, so I left it.

Then I concluded that the signal to noise ratio for his postings dropped
below my threshold...
 
R

Richard

Default User said:
Is it not blindingly obvious by now that Richard is a troll? Why
respond to this sort of thing?

Interesting.

I mention the boring, repetitive posts about usenet "standards" in
this NG and *I* am the troll?

You need to grow up or get some perspective.
 
R

Richard

Mark Bluemel said:
It's a little more complex than that. I struggled for a while with
whether or not to killfile Richard, and checked in Google Groups to
see whether he actually posted useful stuff at all. The evidence there
seemed to indicate that he did, so I left it.

Then I concluded that the signal to noise ratio for his postings
dropped below my threshold...

OT. Not relevant to this NG. Blah Blah Blah.
 
R

Richard Heathfield

Richard said:

I mention the boring, repetitive posts about usenet "standards"

That's almost all you ever mention.
in this NG and *I* am the troll?

Yup. If you want to change this image, first you must change your
behaviour. If you don't change the behaviour, you'll keep the image.
You need to grow up or get some perspective.

See?
 
R

Richard

Richard Heathfield said:
Richard said:



That's almost all you ever mention.

That' "almost" all there is on some days. Well, that and you patting
yourself on the back.
Yup. If you want to change this image, first you must change your
behaviour. If you don't change the behaviour, you'll keep the image.


See?

No. No I don't. If I feel i can contribute to a thread I will. I dont
feel the need to pile in with the same advice that others have posted 37
times in order to keep my tally up.

It seems you do.

See?
 
R

Richard Heathfield

Richard said:
That' "almost" all there is on some days. Well, that and you patting
yourself on the back.

Actually, I don't do that. For one thing, I can't twist my arm around
that far. There are plenty of C threads to which you could contribute
positively if you chose.
No. No I don't. If I feel i can contribute to a thread I will. I dont
feel the need to pile in with the same advice that others have posted
37 times in order to keep my tally up.

It seems you do.

No, I generally don't bother to answer a question that has already been
answered, and I'm not interested in tallies. In fact, since I modded
down gmail users by default because of the recent heavy influx of
abusive posts originating from gmail accounts, I am answering fewer and
fewer OP questions here in the first place.

I see that you continue to misunderstand this group.
 

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
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top