How to Use C Struct

How to Use C Struct - How to Use C Struct's... Tidy...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: How to Use C Struct's... Tidy programs are a blessing to programmers. Tidy data are just as important. As programs become increasingly complex, their data also grow in complexity and single, independent variables or arrays are no longer enough. What one then needs is a data structure. This is where a new type of variable comes in: it is called a struct type, or in other languages, a record. struct types or structures are usually lumped together with another type of variable called a union. In fact their purposes are quite different. A structure is a package of one or usually more variables which are grouped under a single name. Structures are not like arrays: a structure can hold any mixture of different types of data: it can even hold arrays of different types. A structure can be as simple or as complex as the programmer desires. The word struct is a reserved word in C and it represents a new data type, called an aggregate type. It is not any single type: the purpose of structures is to offer a tool for making whatever shape or form of variable package that a programmer wishes. Any particular structure type is given a name, called a structure-name and the variables (called members) within a structure type are also given names. Finally, every variable which is declared to be a particular structure type has a name of its own too. This plethora of names is not really as complicated as it sounds. A structure is declared by making a blank template for a variable package. This is most easily seen with the help of an example. The following statement is actually a declaration, so it belongs with other declarations, either at the head of a program or at the start of a block. struct PersonalData { char name[namesize]; char address[addresssize]; int YearOfBirth; int MonthOfBirth; int DayOfBirth; }; This purpose of this statement is to create a model or template to define what a variable of type struct PersonalData will look like. It says: define a type of variable which collectively holds a string called name, a string called address and three integers called YearOfBirth , MonthOfBirth and DayOfBirth . Any variable which is declared to be of type struct PersonalData will be collectively made up of parts like these. The list of variable components which make up the structure are called the members of the structure: the names of the members are not the names of variables, but are a way of naming the parts which make up a structure variable. (Note: a variable which has been declared to be of type struct something is usually called just a structure rather than a structure variable. The distinction is maintained here in places where confusion might arise.) The names of members are held separate from the names of other identifiers in C, so it is quite possible to have variable names and struct member names which are the same. Older compilers did not support this luxury....
View Full Document

This document was uploaded on 02/08/2012.

Page1 / 14

How to Use C Struct - How to Use C Struct's... Tidy...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online