As a matter of academic interest only, is there a way to programmaticall y
list the 'c' data types? I am not looking for detail, just if it is
possible, and what function could be used to accomplish it. For example:
int main void()
{
while there are more data types
{
print next data type;
}
return(0);
}
output:
int
long
float
char
etc. 11 3197
"theshowmecanuc k" <bi***@dev.null > wrote in message
news:pa******** *************** *****@dev.null. .. As a matter of academic interest only, is there a way to programmaticall y list the 'c' data types? I am not looking for detail, just if it is possible, and what function could be used to accomplish it. For example:
int main void()
int main(void)
{ while there are more data types { print next data type; } return(0); }
output: int long float char etc.
[Presumably you're ruling out struct, union, pointer, array and function
types.]
There is no function to do this. You could write your own in C90, since
there are a fixed number of basic data types. You could probably write a
program which writes a program under C99, assuming any extended integer
types will be covered under the xxx_leastN_t types.
That said, why would you want to do this? Even 'academic interest' is only
useful if there's something to be derived from it.
--
Peter
>> As a matter of academic interest only, is there a way to programmaticall y list the 'c' data types? I am not looking for detail, just if it is possible, and what function could be used to accomplish it. For example:
I have occasionally thought about adding a type_t data type to C, which
contains the type of something, and could be used to do run-time
data typing (among other things, you could cast something to a type
specified by a type_t variable). Question: how many bits does it need?
[Presumably you're ruling out struct, union, pointer, array and function types.]
I'm not ruling out those types, and int [2][3][4][5][6] is
different from int [3][2][4][5][6]. It's things like this that make
type_t more than a bit impractical.
There is no function to do this. You could write your own in C90, since there are a fixed number of basic data types. You could probably write a program which writes a program under C99, assuming any extended integer types will be covered under the xxx_leastN_t types.
Since C does not place any upper limit on things like the maximum number
of levels of indirection on pointer to pointer to pointer ... to char
variables, there's a real problem the program would never terminate.
Another challenge related to my type_t idea: given a series of functions
that operate on a type_t, such as type_is_array() , type_is_functio n(),
type_is_pointer (), which tell you the characteristics of a type, and
some operators on types like type_dereferenc e(), which returns the
type you get when you dereference its argument, and type_elementtyp e(),
which returns the type of the elements of an array type, write a program
which prints out a C declaration for that type, using a supplied variable
name and type_t.
You might see code like this in a simplified version that only had
to worry about basic data types and pointers to them (not arrays,
functions, structs, unions, etc.):
for (t = value; type_is_pointer (t); t = type_dereferenc e(t)) {
printf("*");
}
Gordon L. Burditt
Gordon Burditt wrote: As a matter of academic interest only, is there a way to programmaticall y list the 'c' data types? I am not looking for detail, just if it is possible, and what function could be used to accomplish it. For example:
I have occasionally thought about adding a type_t data type to C, which contains the type of something, and could be used to do run-time data typing (among other things, you could cast something to a type specified by a type_t variable). Question: how many bits does it need?
IMHO, it must be of variable length, you must invent some coding for it
to be able to get the type reading the stream of chars. (so fo ex.
type_t must be typedefed as pointer to char).
The other question is why do you want to list all types. I think that
the only use of type_t object is to set it to the type of the other
existing object. Casting to the explicitely defined type is allready
implemented. Casting to some random type is of no use.
--
vir
>> I have occasionally thought about adding a type_t data type to C, which contains the type of something, and could be used to do run-time data typing (among other things, you could cast something to a type specified by a type_t variable). Question: how many bits does it need?
IMHO, it must be of variable length, you must invent some coding for it to be able to get the type reading the stream of chars. (so fo ex. type_t must be typedefed as pointer to char).
The other question is why do you want to list all types. I think that
That was the original poster. I think the list, not taking into
account implementation restrictions, is of infinite size. Even if
it's not of infinite size, if arrays of 31 dimensions are allowed,
you have something like MAXINT to the power of 31 different possible
31-dimensional arrays of char, which is still impractical to list.
the only use of type_t object is to set it to the type of the other existing object. Casting to the explicitely defined type is allready implemented. Casting to some random type is of no use.
You can pass a type_t and a value of that type to a varargs function.
This gives more complete information than the "printf-style string"
method, and it's more convenient. (Consider:
debug_print_any _type(typeof(fo o), foo) which could, for example,
accurately print every member of a structure (with element names!
and using the names of enums if there is an enum element in the
structure) if foo is a structure, and print the correct number of
array elements if foo is an array) Given that you've got a "plug-in"
function (which some platforms support, but it's not portable) that
takes a particular set of arguments, you could construct those
arguments.
A "new varargs" function call could *AUTOMATICALLY* pass the types
of its arguments. Some people would argue that this is too much
overhead, and they may be right, which is why I'm not suggesting
that existing varargs functions be converted.
You can construct type_t's at runtime by building them up from user
input (effectively, VLA elements of structures). You can build,
then use, types (structures) from other descriptions of data
structures (e.g. from SQL database schema). You can accurately
(and, if this proposal was implemented, portably) calculate the
size of a structure containing a char, a pointer(N)-to-char (This
means N levels of indirection, N being specified at runtime), a double,
and an array of 5 ints *INCLUDING ALL THE PADDING* (along with the
temperature in Hell), and even calculate which ordering of these
gives the least amount of padding.
Gordon L. Burditt
Gordon Burditt wrote: That was the original poster. I think the list, not taking into account implementation restrictions, is of infinite size. Even if it's not of infinite size, if arrays of 31 dimensions are allowed, you have something like MAXINT to the power of 31 different possible 31-dimensional arrays of char, which is still impractical to list.
I think C types are an infinit, enumeratable set. So there are an
infinit amount of types, but there *is* a method to enumerate them, so
there *is* a computable function to map any natural number to the C type. You can pass a type_t and a value of that type to a varargs function. This gives more complete information than the "printf-style string" method, and it's more convenient. (Consider: debug_print_any _type(typeof(fo o), foo) which could, for example, accurately print every member of a structure (with element names! and using the names of enums if there is an enum element in the structure) if foo is a structure, and print the correct number of array elements if foo is an array) Given that you've got a "plug-in" function (which some platforms support, but it's not portable) that takes a particular set of arguments, you could construct those arguments.
The Std ó Library printf functions have an advantage of compact writing
and the ability to store format strings in the files in portable
(enough) way. So your scheme hadn't any of this advantagies and besides
function prototypes store the types of their arguments. So the Modula-2
and C++ style is much more apropriate for this kind of task.
WrInt (5); WrStr("Hellow World");
And this style doesn't invole such complexities as your scheme. You can construct type_t's at runtime by building them up from user input (effectively, VLA elements of structures). You can build, then use, types (structures) from other descriptions of data structures (e.g. from SQL database schema). You can accurately (and, if this proposal was implemented, portably) calculate the size of a structure containing a char, a pointer(N)-to-char (This means N levels of indirection, N being specified at runtime), a double, and an array of 5 ints *INCLUDING ALL THE PADDING* (along with the temperature in Hell), and even calculate which ordering of these gives the least amount of padding.
So It's very hard to me to imaging any particular code (using only
casting and type_t objects). Give an example :). How will you use an
object of the unknown (at the time of compilation) type. And if you
willn't, then void pointer exists. Store anything you want.
But one proposal (not yours as I see) I agree with is to have a constant
to store maximum padding of C objects, to be able to write effective by
space allocators.
--
vir
On 25 Jul 2004 08:17:27 GMT, go***********@b urditt.org (Gordon
Burditt) wrote in comp.lang.c: As a matter of academic interest only, is there a way to programmaticall y list the 'c' data types? I am not looking for detail, just if it is possible, and what function could be used to accomplish it. For example:
I have occasionally thought about adding a type_t data type to C, which contains the type of something, and could be used to do run-time data typing (among other things, you could cast something to a type specified by a type_t variable). Question: how many bits does it need?
[Presumably you're ruling out struct, union, pointer, array and function types.]
I'm not ruling out those types, and int [2][3][4][5][6] is different from int [3][2][4][5][6]. It's things like this that make type_t more than a bit impractical.
There is no function to do this. You could write your own in C90, since there are a fixed number of basic data types. You could probably write a program which writes a program under C99, assuming any extended integer types will be covered under the xxx_leastN_t types.
Since C does not place any upper limit on things like the maximum number of levels of indirection on pointer to pointer to pointer ... to char variables, there's a real problem the program would never terminate.
Another challenge related to my type_t idea: given a series of functions that operate on a type_t, such as type_is_array() , type_is_functio n(), type_is_pointer (), which tell you the characteristics of a type, and some operators on types like type_dereferenc e(), which returns the type you get when you dereference its argument, and type_elementtyp e(), which returns the type of the elements of an array type, write a program which prints out a C declaration for that type, using a supplied variable name and type_t.
You might see code like this in a simplified version that only had to worry about basic data types and pointers to them (not arrays, functions, structs, unions, etc.):
for (t = value; type_is_pointer (t); t = type_dereferenc e(t)) { printf("*"); }
Gordon L. Burditt
Why do you think this is so difficult or never ending? Compilers do
it all the time.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++ http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
>> You can construct type_t's at runtime by building them up from user input (effectively, VLA elements of structures). You can build, then use, types (structures) from other descriptions of data structures (e.g. from SQL database schema). You can accurately (and, if this proposal was implemented, portably) calculate the size of a structure containing a char, a pointer(N)-to-char (This means N levels of indirection, N being specified at runtime), a double, and an array of 5 ints *INCLUDING ALL THE PADDING* (along with the temperature in Hell), and even calculate which ordering of these gives the least amount of padding.So It's very hard to me to imaging any particular code (using only casting and type_t objects). Give an example :). How will you use an object of the unknown (at the time of compilation) type. And if you willn't, then void pointer exists. Store anything you want.
Write me a routine to dump a struct foobaz, using a void pointer
to it as input. I won't write the definition of struct foobaz until
after you submit and compile your code. Raw hexadecimal dumps don't
count.
But one proposal (not yours as I see) I agree with is to have a constant to store maximum padding of C objects, to be able to write effective by space allocators.
I'm assuming that there are also a large variety of operators on
type_t objects so you can construct your own types and heavily pick
apart existing types you are passed. It also includes the ability
to reference structure members by runtime-specified names (which
you can extract out of the type_t).
Imagine that you are writing a database. Using the schema for a
particular table, you construct a type_t for a structure corresponding
to the schema. Now you can take a void * pointer, cast it to a
pointer to this struct, dereference it, and use sizeof() on it.
Now you've got the size to allocate, WITH PADDING. You can also
access elements of this structure using the cast pointer. For
optimization, you can try all possible orders of structure members
and use the one with the least padding.
My proposal for "new varargs" (which implicitly passes the type of
the argument along with the argument) leaves open the possibility of
things you can't currently do in C:
- A version of printf() that actually CHECKS the compatability of the
format argument and the argument passed.
- A version of gets() (or scanf()) that takes an array argument,
refuses to overrun that array, and will fail if you pass it a pointer
instead.
- A version of execl() which takes an arbitrary number of strings
and DOESN'T need a NULL pointer to mark the end of the arguments.
Gordon L. Burditt
Gordon Burditt wrote: Write me a routine to dump a struct foobaz, using a void pointer to it as input. I won't write the definition of struct foobaz until after you submit and compile your code. Raw hexadecimal dumps don't count.
It is imposible right now, but it is very easy using some conventions
like inheritance in OOP sence.
Imagine that you are writing a database. Using the schema for a particular table, you construct a type_t for a structure corresponding to the schema. Now you can take a void * pointer, cast it to a pointer to this struct, dereference it, and use sizeof() on it. Now you've got the size to allocate, WITH PADDING. You can also access elements of this structure using the cast pointer. For optimization, you can try all possible orders of structure members and use the one with the least padding.
So your proposal is to include the hole database managment code into
every C program, and to make it the part of the C compiler. This is an
ambigous task. And the only problem is that I know very little (none)
peaple who needs it. There *is* an existing databases and they work, use
one. The object to hold type information is called SQL query.
Try to figure out what you really need. Do you want to implement
something that really requires your run-time type handling. Do you think
that the code using type_t will be simplier or smaller then that using
C++ with it's run-time type handling being developed scince 70th? Do you
think that your type_t is better than all OOP techniques?
My proposal for "new varargs" (which implicitly passes the type of the argument along with the argument) leaves open the possibility of things you can't currently do in C:
- A version of printf() that actually CHECKS the compatability of the format argument and the argument passed.
Good idea but do you expect to include the code for printing every
possible C type (infinity) to a Standart library. - A version of gets() (or scanf()) that takes an array argument, refuses to overrun that array, and will fail if you pass it a pointer instead.
Write some wrapers for string and scanf functions using
struct {
void *ptr;
size_t size;
};
and you are done. It's much easier then implementing general run-time
type handling.
- A version of execl() which takes an arbitrary number of strings and DOESN'T need a NULL pointer to mark the end of the arguments.
Does ir really bother you?
--
vir
>> I have occasionally thought about adding a type_t data type to C, which contains the type of something, and could be used to do run-time data typing (among other things, you could cast something to a type specified by a type_t variable). Question: how many bits does it need?
>[Presumably you're ruling out struct, union, pointer, array and function >types.]
I'm not ruling out those types, and int [2][3][4][5][6] is different from int [3][2][4][5][6]. It's things like this that make type_t more than a bit impractical.
>There is no function to do this. You could write your own in C90, since >there are a fixed number of basic data types. You could probably write a >program which writes a program under C99, assuming any extended integer >types will be covered under the xxx_leastN_t types.
Since C does not place any upper limit on things like the maximum number of levels of indirection on pointer to pointer to pointer ... to char variables, there's a real problem the program would never terminate.
Another challenge related to my type_t idea: given a series of functions that operate on a type_t, such as type_is_array() , type_is_functio n(), type_is_pointer (), which tell you the characteristics of a type, and some operators on types like type_dereferenc e(), which returns the type you get when you dereference its argument, and type_elementtyp e(), which returns the type of the elements of an array type, write a program which prints out a C declaration for that type, using a supplied variable name and type_t.
You might see code like this in a simplified version that only had to worry about basic data types and pointers to them (not arrays, functions, structs, unions, etc.):
for (t = value; type_is_pointer (t); t = type_dereferenc e(t)) { printf("*"); }
Gordon L. Burditt
Why do you think this is so difficult or never ending? Compilers do it all the time.
Compilers don't have to represent a type in a type_t which is a single
variable with a bounded amount of memory determined at compile time.
It would be fairly easy to represent a type as a linked list, where one
node in the list might be a basic type, or "pointer to <this>", or
"array [<this>] of <that", or "function returning <this> and taking
arguments <that1>, <that2>, and <that3>". You might even manage to
have only one node for each basic type, everything else just points to
it.
However, I don't think it is reasonable that:
type_t t;
for (t = typeof(char); ; t = type_reference( t) ) {
/* do something with t */
}
(t would take on the types char, char *, char **, char ***, and so on.)
fails because of running out of memory.
Gordon L. Burditt This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics | |
by: Anon Email |
last post by:
Hi people,
I'm learning about header files in C++. The following is code from
Bartosz Milewski:
// Code
const int maxStack = 16;
class IStack
|
by: GrahamJWalsh |
last post by:
I have a basic question regarding various data types (related to
reinterpret_cast, static_cast).
Lets say I have some declarations thus;
int i = 444;
unsigned int* uip;
I then do something like;
|
by: Dave |
last post by:
We are trying to migrate a MS SQL server app to DB2 8.1 Linux platform.
Our database has got about 300+tables with total size - 150 GB
We are using MS SQL's BCP utility to extract data from MS SQL's tables
and loading into DB2
using DB2's LOAD utility. There are tons of colums of floating point
types (singe precion & double precision types)
in the database and when extracted using BCP, it generates data only upto
17 digits....
|
by: Craig |
last post by:
Hi everyone,
As a relative new-comer to the wonderful world of .NET Framework and
the C# langauge I have come across something that I would like to
clarify (hopefully with the help of kind people such as yourselves).
To quote, "Everything in C# is an object" (including "primitive" value
types, primitive being in quotations because this is what my question
concerns).
|
by: Rookie |
last post by:
Hi,
I had a question on DllImport. On importing a function from a VC++ dll
using DllImport (to a C# program), the function argument data types
and the return types may be of a type that is not supported by C#. In
this case if I am not mistaken the system performs default marshalling
- matching the data types to its most similar equivalent in C#. Is
this right?
Also, I presume this can be overridden by using MarshalAs. Is this
| | |
by: Arpan |
last post by:
The .NET Framework 2.0 documentation states that
An Object variable always holds a pointer to the data, never the data
itself.
Now w.r.t. the following ASP.NET code snippet, can someone please
explain me what does the above statement mean?
<script runat="server">
Class Clock
|
by: Joel Hedlund |
last post by:
Hi!
The question of type checking/enforcing has bothered me for a while, and
since this newsgroup has a wealth of competence subscribed to it, I
figured this would be a great way of learning from the experts. I feel
there's a tradeoff between clear, easily readdable and extensible code
on one side, and safe code providing early errors and useful tracebacks
on the other. I want both! How do you guys do it? What's the pythonic
way? Are...
|
by: aarklon |
last post by:
Hi all,
this is a question which i saw in a book
typedef struct mall_li_header_ {
int refcnt;
uchar pool;
uchar flag;
ushort magic_no;
char data;
|
by: Madhur |
last post by:
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={int,float,char,int*..............................},
so that a should return me int and a should return me
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| | |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| | |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |