The typedef Statement in C Programming Language

C provides a capability that enables you to assign an alternate name to a data type. This is done with a statement known as typedef. The statement

typedef int Counter;

defines the name Counter to be equivalent to the C data type int.Variables can subse- quently be declared to be of type Counter, as in the following statement:

Counter j, n;

The C compiler actually treats the declaration of the variables j and n, shown in the preceding code, as normal integer variables. The main advantage of the use of the typedef in this case is in the added readability that it lends to the definition of the variables. It is clear from the definition of j and n what the intended purpose of these variables is in the program. Declaring them to be of type int in the traditional fashion would not have made the intended use of these variables at all clear. Of course, choosing more meaningful variable names would have helped as well!

In many instances, a typedef statement can be equivalently substituted by the appro- priate #define statement. For example, you could have instead used the statement

#define Counter int

to achieve the same results  as the preceding statement. However, because the typedef is handled by the C compiler proper, and not by the preprocessor, the typedef statement provides more flexibility than does the #define when it comes to assigning names to derived data types. For example, the following typedef statement:

typedef char Linebuf [81];

defines a type called Linebuf, which is an array of 81 characters. Subsequently declaring variables to be of type Linebuf, as in

Linebuf text, inputLine;

has the effect of defining the variables text and inputLine to be arrays containing 81 characters. This is equivalent to the following declaration:

char text[81], inputLine[81];

Note that, in this case, Linebuf could not have been equivalently defined with a #define preprocessor statement.

The following typedef defines a type name StringPtr to be a char pointer:

typedef char *StringPtr;

Variables subsequently declared to be of type StringPtr, as in

StringPtr buffer;

are treated as character pointers by the C compiler.

To define a new type name with typedef, follow these steps:

  1. Write the statement as if a variable of the desired type were being declared.
  2. Where the name of the declared variable would normally appear, substitute the new type name.
  3. In front of everything, place the keyword typedef.

As an example of this procedure, to define a type called Date to be a structure contain- ing three integer members called month, day, and year, you write out the structure defi- nition, substituting the name Date where the variable name would normally appear (before the last semicolon). Before everything, you place the keyword typedef:

typedef struct

{

int month;

int day;

int year;

} Date;

With this typedef in place, you can subsequently declare variables to be of type Date, as in

Date birthdays[100];

This defines birthdays to be an array containing 100 Date structures.

When working on programs in which the source code is contained in more than one file (as described  in Chapter 15, “Working with Larger Programs”), it’s a good idea to place the common typedefs into a separate file that can be included into each sourcebfile with an #include statement.

As another example, suppose you’re working on a graphics package that needs to deal with drawing lines, circles, and so on.You probably will be working very heavily with the coordinate system. Here’s a typedef statement that defines a type named Point, where a Point is a structure containing two float members x and y:

typedef struct

{

float x;

float y;

} Point;

You can now proceed to develop your graphics library, taking advantage of this Point

type. For example, the declaration

Point origin = { 0.0, 0.0 }, currentPoint;

defines origin and currentPoint to be of type Point and sets the x and y members of origin to 0.0.

Here’s a function called distance that calculates the distance between two points.

#include <math.h>

double distance (Point p1, Point p2)

{

double diffx, diffy;

diffx = p1.x – p2.x;

diffy = p1.y – p2.y;

return sqrt (diffx * diffx + diffy * diffy);

}

As previously noted, sqrt is the square root function from the standard library. It is declared in the system header file math.h, thus the reason for the #include.

Remember, the typedef statement does not actually define a new type—only a new type name. So the Counter variables j and n, as defined in the beginning of this section, would in all respects be treated as normal int variables by the C compiler.

Source: Kochan Stephen G. (2004), Programming in C: A Complete Introduction to the C Programming Language, Sams; Subsequent edition.

Leave a Reply

Your email address will not be published. Required fields are marked *