UMBC CS 201, Fall 06
UMBC CMSC 201
Fall '06

CSEE | 201 | 201 F'06 | lectures | news | help

Confusion about similar syntax
involving structure definitions

typedefs and structures

As we have seen, typedef is commonly used by programmers to make an alias for a struct type when it is defined. The syntax is : typedef struct point { int x; int y; } POINT; so here POINT is a data type, identical to a struct point.

structure definitions & variable declarations

Since we use separate compilation for all of our projects, and the structure definitions end up being placed in header files, we will never encounter the following use of a structure definiton with an associated declaration of a variable. You should, however, be aware that it is possible to declare variables of a struct type at the same time as the stucture is defined. Here's the example: struct point { int x; int y; } POINT; Here POINT is a variable of type struct point, not a type. I broke the 201 naming conventions here to illustrate and further compound the confusion.

So how do you know if POINT is a type being typedefed or just a variable being declared ?

If the word typedef does not appear before the definition, then a variable is being declared.


CSEE | 201 | 201 F'06 | lectures | news | help

Tuesday, 22-Aug-2006 07:14:09 EDT