typedef

V

venky

Hi All,

I am facing problem with typedef, can you explain in terms of real
time use of typdef?

Thank you
 
O

osmium

venky said:
I am facing problem with typedef, can you explain in terms of real
time use of typdef?

First, note that "real time" has a special meaning in the computer field
that I don't think you intended here.

Secondly, note that typedef is a spectacularly bad word choice since it
doesn't define a type, rather it provides a new name, a synonym, for an
existing type. Once the typedef has been seen by the compiler you can use
either the old name or the new name for the same type.

Two dominant uses come to mind. One is to hide or encapsulate the
complexity of a complicated type, for example the pointer to a function is
often a rather nasty looking thing. Pointers to functions are typically
encountered in more or less advanced work, but beginners can encounter them,
for example, in the parameter list for bsearch and qsort in <stdlib.h>.

The other use is to minimize typing, a use frowned upon by many. For
example:

typedef unsigned char UC.

In the sequel you can write:

UC ch;

It makes virtually no sense for a single char, of course.

When you prefix a variable declaration with "typedef" the variable therein
is "promoted" to a type. I like to capitalize, at least the initial letter,
of types.
 
E

ena8t8si

osmium said:
Two dominant uses [of typedef] come to mind. One is to hide or encapsulate the
complexity of a complicated type, for example the pointer to a function is
often a rather nasty looking thing. Pointers to functions are typically
encountered in more or less advanced work, but beginners can encounter them,
for example, in the parameter list for bsearch and qsort in <stdlib.h>.

The other use is to minimize typing, a use frowned upon by many.
[and a poor motivation it is...]

You missed a more important motivation for using typedef,
which is to document the nature of variables of some type.
For example,

typedef unsigned long Dollars;
typedef unsigned long Yen;
typedef unsigned long Length;
typedef unsigned long ElapsedTime;

Even though all of these are represented as an unsigned long,
using a typedef name documents the kind of usage expected
and can make programs easier to comprehend.

Obviously more complicated examples are possible but I
think these simple ones illustrate the point.
 
O

osmium

osmium said:
Two dominant uses [of typedef] come to mind. One is to hide or
encapsulate the
complexity of a complicated type, for example the pointer to a function
is
often a rather nasty looking thing. Pointers to functions are typically
encountered in more or less advanced work, but beginners can encounter
them,
for example, in the parameter list for bsearch and qsort in <stdlib.h>.

The other use is to minimize typing, a use frowned upon by many.
[and a poor motivation it is...]

You missed a more important motivation for using typedef,
which is to document the nature of variables of some type.
For example,

typedef unsigned long Dollars;
typedef unsigned long Yen;
typedef unsigned long Length;
typedef unsigned long ElapsedTime;

Even though all of these are represented as an unsigned long,
using a typedef name documents the kind of usage expected
and can make programs easier to comprehend.

Obviously more complicated examples are possible but I
think these simple ones illustrate the point.

Sometimes I think even a socket wrench could be misused.
 

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
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top