By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,101 Members | 1,463 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,101 IT Pros & Developers. It's quick & easy.

Creating array of data types

P: n/a
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

Jul 24 '07 #1
Share this Question
Share on Google+
15 Replies


P: n/a
Madhur wrote:
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.
Jul 24 '07 #2

P: n/a
Mark Bluemel said:

<snip>
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.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jul 24 '07 #3

P: n/a
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????


On Jul 24, 1:34 pm, Mark Bluemel <mark_blue...@pobox.comwrote:
Madhur wrote:
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.

Jul 24 '07 #4

P: n/a
Madhur wrote:
Thanks for the info...
Please don't top post.
>
On Jul 24, 1:34 pm, Mark Bluemel <mark_blue...@pobox.comwrote:
>>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...
Jul 24 '07 #5

P: n/a
On Jul 24, 2:24 pm, Mark Bluemel <mark_blue...@pobox.comwrote:
Madhur wrote:
Thanks for the info...

Please don't top post.


On Jul 24, 1:34 pm, Mark Bluemel <mark_blue...@pobox.comwrote:
>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...
which brings again back to the question...

Jul 24 '07 #6

P: n/a
Madhur wrote:
On Jul 24, 2:24 pm, Mark Bluemel <mark_blue...@pobox.comwrote:
>>Madhur wrote:
>>>Thanks for the info...

Please don't top post.

>>>On Jul 24, 1:34 pm, Mark Bluemel <mark_blue...@pobox.comwrote:
>>>>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.
Jul 24 '07 #7

P: n/a
Madhur <ma********@gmail.comwrites:
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) ks***@mib.org <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"
Jul 24 '07 #8

P: n/a
On Jul 25, 12:13 am, Keith Thompson <ks...@mib.orgwrote:
Madhur <madhurr...@gmail.comwrites:
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) ks...@mib.org <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++..

Jul 25 '07 #9

P: n/a
Madhur wrote:
On Jul 25, 12:13 am, Keith Thompson <ks...@mib.orgwrote:
<snip>
>--
Keith Thompson (The_Other_Keith) ks...@mib.org
<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"
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?

Jul 25 '07 #10

P: n/a
santosh <sa*********@gmail.comwrites:
Madhur wrote:
>On Jul 25, 12:13 am, Keith Thompson <ks...@mib.orgwrote:

<snip>
>>--
Keith Thompson (The_Other_Keith) ks...@mib.org
<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"

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.
>
>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?
I have one. Why did you bother to reply?
Jul 25 '07 #11

P: n/a
On Jul 24, 3:25 am, Madhur <madhurr...@gmail.comwrote:
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.

Jul 25 '07 #12

P: n/a
Richard wrote:
santosh <sa*********@gmail.comwrites:
>Madhur wrote:
>>On Jul 25, 12:13 am, Keith Thompson <ks...@mib.orgwrote:

<snip>
<snip>
>Don't quote signatures unless you're commenting on them.

So don't requote them.
If the OP is new to Usenet, he might not know what a signature is, hence I
placed my comments just below one, as a hint.

Jul 25 '07 #13

P: n/a
santosh wrote:
Richard wrote:
santosh <sa*********@gmail.comwrites:
Madhur wrote:
>
On Jul 25, 12:13 am, Keith Thompson <ks...@mib.orgwrote:
>
<snip>

<snip>
Don't quote signatures unless you're commenting on them.
So don't requote them.

If the OP is new to Usenet
Is it not blindingly obvious by now that Richard is a troll? Why
respond to this sort of thing?


Brian
Jul 25 '07 #14

P: n/a
Default User wrote:
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...
Jul 26 '07 #15

P: n/a
Mark Bluemel <ma**********@pobox.comwrites:
Default User wrote:
>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...
OT. Not relevant to this NG. Blah Blah Blah.

Jul 26 '07 #16

This discussion thread is closed

Replies have been disabled for this discussion.