Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

The GNU C Programming Tutorial - Structure declarations using typedef

Node:Structure declarations using typedef, Previous:Structure declarations, Up:Structure declarations



Structure declarations using typedef

Alternatively, the typedef command can be used to cut down on typing out code in the long term. The type definition is made once at the start of the program and subsequent variable declarations are made by using the new name, without the word struct:

typedef struct
{
  char name[100];
  char address[200];
  int year_of_birth;
  int month_of_birth;
  int day_of_birth;
} personal_data;

personal_data person001;
personal_data person002;
personal_data person003;

Note that this use of the typedef command parallels the usage we have already seen:

typedef existing_type new_type

In the example above of using typedef to declare a new type of structure, the metasyntactic variable new_type corresponds to the identifier personal_data, and the metasyntactic variable existing_type corresponds to the following code:

struct
{
  char name[100];
  char address[200];
  int year_of_birth;
  int month_of_birth;
  int day_of_birth;
}

Structure type and variable declarations can be either local or global, depending on their placement in the code, just as any other declaration can be.

 
 
  Published under free license. Design by Interspire