Dunfield Development Services Inc.
Customer Support -- Application Note #0006
MICRO-C
Simulating typedef'd structures with #define

Applies to: [Micro-C Compiler
]
Last updated: Sunday May 04, 2003
.
Application Note Index
[ Back ] [ Next ]

PROCEDURE
It is a common practice among many programmers to use "typedef" to declare general structures. Many people do this for no other reason than that it
allows you to declare subsequent structure variables without "struct":
typedef struct {
int member1;
int member2; } structtype; /* Define a type for the structure */
structtype structvar; /* Define variable containing struct */
It is my opinion that this is bad programming practice (See note below), and such structures would be more properly declared with the
"struct"
directive... After all, that is what "struct" is for:
struct structtype { /* Declare structure temmplate */
int member1;
int member2; };
struct structtype structvar; /* Define variable containing struct */
Since MICRO-C does not support "typedef", the former declarations will not work, and you must use the latter form. This is not a problem with new code,
however it can become a problem if you are porting a large source with many structure variable declarations to use with MICRO-C. It is usually relatively
easy to modify the "typedef" of the structure type, but finding and changing all of the structure variable declarations can be tedious. Here is a simple
way of using the C preprocessor to define structures using "struct" that can be declared with a single type specified in the source:
#define structtype struct struct_type
structtype { /* Define structure template */
int member1;
int member2; };
structtype structvar; /* Define variable containing struct */
Note that in this example, the actual declaration of the structure variable is the same as the
"typedef" example at the beginning of this
document. Using this technique, you can port sources using "typedef" to declare structures by modifying only the type declarations, not the
individual variable declarations.
Also note that if you do not wish to use the MCP preprocessor, you can use this technique with the limited internal preprocessor by modifying the
definition to read: #define structtype struct/**/struct_type
Why typedef should not be used for general structure definitions:
Typedef's were added to the C language for the purpose of enhancing source code portability by allowing different types required by different operating
systems (or implementations of any type of system) to be created and passed to library functions with the same syntax. They specifically are intended to
hide information about the content and layout of the associated variables. For example, you do not need to know that
"stdin" points to an integer on
one system, a data structure on another, and a character array on a third. You simply declare and manipulate the user defined type FILE. This hiding
of complexity is good because every C programmer knows the purpose of FILE, and how it should behave. This standardized and expected behaviour is not
known however for user defined entities.
To use "typedef" simply to avoid typing "struct" in declarations is simple laziness, and actually reduces the
readability of your program. It hides information (that this item is a structure). Typedef should be used only
where the derived variable type is implementation dependant, or unique enough that it can be considered as separate programming unit. Many (most) cases
where I have seen typedef used for structure declarations do not fit these categories.
|
|