Introduction
Now is a good time to review the concept on struct
, enum
, union
and namespace
in C language. If you have feedback or comments, please provide them. Thank you.
To save time, many examples are classical and taken from [1] or [2]. I do not re-invent the wheel.
Quick Check
Can you interpret the following statement correctly?
typedef int *ptr, (*func)(), arr[5];
If you cannot interpret 100% correctly, please read on.
Namespace in C
There are multiple namespace
s in C[1]:
- label names
- tags (one
namespace
for all struct
s, enum
s and union
s) - member names (each
struct
or union
has its own namespace
) - everything else
Within a namespace
, every identifer must be unique. An identical identifier can be applied to things in different namespace
s. Because each struct
or union
has its own namespace
, the same member names can be re-used in many different struct
s.
The following example is valid:
struct foo {int foo;} foo;
typedef with Function Pointer
A classic example is the signal()
prototype. The ANSI standard shows that signal is declared as:
void (*signal(int sig, void (*func)(int) ) (int);
If we use typedef
, we can simplify this declaration:
typedef void (*ptr_to_func) (int);
ptr_to_func signal(int, ptr_to_func);
So we refactor this complicated statement by factoring out the common piece. In a further way, we can simplify our legacy code.
typedef with type struct, enum, and union
Please see if this example is confusing?
typedef struct blahh {int blahh; } blahh;
struct blahh var1;
blahh var2;
The take-away is: do not mess typedef
only with struct
s; We can use typdef
for the types that combine arrays, structs, pointers, or functions; use typedef
for portable types; use typedef
for casts to simplify the complicated type cast.
As for enum
type, it is only a simple way to associate a series of names with a series of integer values. Another angle to say is: if a variable has a fixed set of values, then it is good to declare it enum
type. C is a weakly typed language and enum
type only provides very little that can't be done with #define
. The definition is below:
enum optional_tag {a list of identifers} optional_variable_defintions;
Compared with #define
, enum
type has one advantage: #define
names are typically discarded during compilation and enum
names usually persist through to the debugger and can be used while debugging your code in IDE such as Visual Studio.
#define vs. typedef
We know there is a key difference between typedef
and #define
. I found a better way to express it. A typedef
is a complete "encapsulated" type (you cannot add to it after you declare it)[1]. #define
is a text replacement on the spot and it happens in pre-processing stage in compiling.
Please review the following two examples and see how much you can understand it:
#define fruit int
unsigned fruit i;
typedef int fig;
unsigned fig i;
#define int_ptr int *
int_ptr tiger, cat;
typedef char* char_ptr;
char_ptr saga, novel;
One Use Case of #define
#define
can be used to simplify convoluted statement too. Here, it is the example:
enum type_tag { IDENTIFIER, QUALIFIER, TYPE };
if ( !strcmp(s, "volatile") ) return QUALIFIER;
#define STRCMP(a, R, b) (strcmp(a,b) R 0)
if ( STRCMP(s, ==, "volatile")) return QUALIFIER;
Points of Interest
If you understand these concepts better, you will be more confident to use C. If you get the essence of this article, [3] is the next article to go through.
References
- Expert C Programming: Deep C Secrets 1st Edition
- Why doesn't ANSI C have namespaces?
- Improve Code Clarity with Typedef
- typedef struct vs struct definitions [duplicate]
- Why should we typedef a struct so often in C?
- typedef
- C Preprocessor tricks, tips, and idioms
- The C Preprocessor
- C Programming Tips And Tricks For Beginners – Top 15
- C Programming Tips
- C Pre-Processor Magic
- The C Preprocessor
- C Macro Tips and Tricks
- Easy way to use variables of enum types as string in C?
- Assorted C/C++ tips and tricks.
- Recommended C Style and Coding Standards
- Don’t Follow These 5 Dangerous Coding Standard Rules
- C# Fundamentals: The Joys and Pitfalls of Enums
- 2 Rookie Java Constants and Enums Pitfalls
History
- 27th February, 2017: Initial version