UMBC CS 201, Fall 06
Fall '06

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


(With apologies to K&R)

A structure is a collection of many variables, possibly of different types, grouped together under a single name for convenient handling. Structures allow a group of related variables to be treated as a unit instead of separate entities. Structures are sometimes called "records" in other languages, notably Pascal.

Structure Definition

The keyword struct starts the structure definition, which is a list of declarations enclosed in braces. The variables named in a structure are called members.

The general form of a structure definition is

struct tag { member1_declaration; member2_declaration; member3_declaration; . . . memberN_declaration; }; where struct is the keyword, tag names this kind of struct, and member_declarations are variable declarations which define the members.

An example structure definition

The following structure declaration defines a point as its x- and y-coordinates, both integers. Note that this is just a definition of the structure type. It describes a template or the shape of the structure. It does not declare any variable, or allocate any memory. struct point { int x; /* x-coordinate */ int y; /* y-coordinate */ }; Inside structure point, there are two "members" (aka "fields") defined: x and y.

Declaring a structured variable

Given the structure definition of point above, we can declare the variable point1 to be of type struct point with the statement: struct point point1; If desired, the members of the structure can be initialized: struct point point1 = {3, 6}; Now point1's x-coordinate is 3 and y-coordinate is 6.

Accessing the members

The members of a structure are accessed using the dot (.) operator. The dot operator connects the structure name and the member name. This combination of structure name and member name uniquely identify the member. Hence, a structure member may have the same name as an "ordinary" variable. Further, the same member names may occur in different structures.

Given the declarations

struct point point1; struct point point2; We access the members of these variables as

These members can then be used just like any other integer variables. For example, we can print the x- and y-coordinates of point2 using

printf ("%d, %d", point2.x, point2.y); or we can use scanf to put values into them printf ("Please enter the x-coordinate: "); scanf ("%d", &point2.x);

Structure Assignment

ANSI C allows us to assign one structure to another using the "=" operator. For example struct point p1 = {3, 5}; struct point p2; p2 = p1; results in p2 and p1 having the same x- and y-coordinates.

Uses of Structures

Structures are used to keep data together that belongs together, even if the data is of different types. Information about a student might be stored in a structure whose definition looks like this:
struct student { char lastName[25]; char firstName[25]; char middleInitial[2]; char ssn[12]; int homeStreetNum; char homeStreetName[25]; char homeCity[25]; char homeState[3]; int homeZipCode; char homePhone[14]; char dorm[25]; int roomNum; char campusPhone[14]; int rank; char major[5]; char minor[5]; float gpa; }; As you can see, this structure is quite large already and certainly would still need to have other members added if it really were to keep track of everything the university needed to know about a student.

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

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