Structure and Union

 

Introduction to structure and union

 

Structure

A collection of variables that are functionally related to each other.

2. Each variable that is a member of the structure has a specific type.

3. Different members of the structure may have either the same or different

types. Cf. the elements of an array, which must all be of one type.

4. A structure is a derived data type, constructed from two or more objects of one or more individual types.

5. The entire structure may bear a name.

6. Each member of the structure must [also] have a name.

7. The scope of the name of a structure member is limited to the structure itself and also to any variable declared to be of the structure’s type.

8. THEREFORE, different structures may contain members having the same name; these may be of the same or of different types.

9. A self-referential structure contains a member which is a pointer to the same structure type.

10. Declaration of the structure merely defines the new data type; space is NOT reserved in memory as a result of the declaration.

 

However, declaration of the structure does define how much memory is needed to store each variable subsequently declared to be of the type of the defined structure.

 

Union

 

1.Like a structure, a union is also a derived data type.

2. The members of a union share a single storage space.

3. Only ONE member of each union can be referenced at a time.

4. Amount of space allocated for storage is the amount needed for the largest member of the union.

 

 

Declaration

 

Form of Structure Declaration: 1

(1) Complete definition including assignment of a tag name to the structure.

(2) The tag name is referred to in subsequent declarations of variables of the type so defined.

(3) Each such declaration MUST include the keyword struct AND the name of the user-defined structure type AND the variable name(s).

 

struct nameOfThisStructureType

{

typeOfFirstMember                nameOfFirstMember;

typeOfSecondMember           nameOfSecondMember;

typeOfThirdMember               nameOfThirdMember;

. . .

};

 

struct nameOfThisStructureType       variable1OfThisStructureType,

variable2OfThisStructureType,

. . . ;

Additional variable declarations can subsequently be made for this structure type.

 

 

Form of Structure Declaration: 2

(1) Basic named definition of the structure is effected same as for Alternative 1.

(2) In ADDITION, one or more variables can be declared within the declaration of the structure type to be of the defined type.

(3) Other variables may also be declared subsequently to be of the same type of this structure, using the keyword struct together with the tag name and the variable names.

 

struct nameOfThisStructureType

{

typeOfFirstMember                nameOfFirstMember;

typeOfSecondMember           nameOfSecondMember;

typeOfThirdMember               nameOfThirdMember;

. . .

} variable1OfThisStructureType, variable2OfThisStructureType, . . .;

 

struct nameOfThisStructureType       variable3OfThisStructureType,

variable4OfThisStructureType,

. . . ;

 

 

Form of Structure Declaration: 3

(1) Tag name is not assigned to the structure when the type is declared.

(2) Variables are specified within the structure declaration to be of the defined structure type.

(3) Because of the absence of a tag name for the structure type, there is no means available to ever be able to declare any other variables to be of this same type.

 

struct /* NO NAME ASSIGNED TO THE TYPE */

{

typeOfFirstMember              nameOfFirstMember;

typeOfSecondMember        nameOfSecondMember;

typeOfThirdMember             nameOfThirdMember;

. . .

} variable1OfThisStructureType, variable2OfThisStructureType, . . .;

 

Form of Structure Declaration: Alternative 4

(1) Complete definition of the structure, including assignment to it of a tag name.

(2) Subsequently, the tag name is used in a typedef declaration to assign a second name (i.e., an alias ) to the structure. The alias can then be used in declaring a variable the same way as a native C type name is used, that is, without the keyword struct, i.e., just like int, char, float, etc.

 

struct nameOfThisStructureType

{

typeOfFirstMember              nameOfFirstMember;

typeOfSecondMember        nameOfSecondMember;

typeOfThirdMember             nameOfThirdMember;

. . .

};

typedef struct nameOfThisStructureType AliasForThisStructureType;

 

AliasForThisStructureType            variable1OfThisStructureType,

variable2OfThisStructureType, . . . ;

 

 

Form of Structure Declaration: 5

(1) Complete definition of the structure without assignment of a tag name.

(2) The keyword typedef is used within the declaration of the structure to assign a name (i.e., an alias) to the structure. The structure itself is anonymous, and has only the alias name. The alias can be used in the same way as a native C type name is used , that is, without the keyword struct, i.e., just like int, char, float, etc.

 

typedef struct

{

typeOfFirstMember                nameOfFirstMember;

typeOfSecondMember           nameOfSecondMember;

typeOfThirdMember               nameOfThirdMember;

. . .

} AliasForThisStructureType;

 

AliasForThisStructureType     variable1OfThisStructureType,

variable2OfThisStructureType, . . . ;

 

 

Example 1

enum genders {MALE, FEMALE};

enum studentStatus {FRESHMAN, SOPHOMORE, JUNIOR, SENIOR, POSTBAC};

struct student

{

char firstName[20];

char lastName[20];

char middleName[20];

long int studentNumber;

short int entranceYear;

genders studentGender;

studentStatus status;

char major[6];

struct student *nextStudent; /* Useful for making a linked list. */

struct student *priorStudent; /* Useful for a doubly linked list. */

};

 

struct student undergraduateStudent, graduateStudent;

struct student specialStudent;

 

 

Initializing structure

 

Initializer lists

– Example:

card oneCard = { “Three”, “Hearts” };

• Assignment statements

– Example:

card threeHearts = oneCard;

– Could also define and initialize threeHearts as follows:

card threeHearts;

threeHearts.face = “Three”;

threeHearts.suit = “Hearts”;

 

 

Accessing structure elements

 

– Dot operator (.) used with structure variables

card myCard;

printf( “%s”, myCard.suit );

– Arrow operator (->) used with pointers to structure variables

card *myCardPtr = &myCard;

printf( “%s”, myCardPtr->suit );

– myCardPtr->suit is equivalent to

( *myCardPtr ).suit

 

 

Array of structure

 

The inventory and the label program examples of the last section handle only a single record. More realistically, a useful program may need to handle many such records. As in previous cases where we needed to store a list of items, we can use an array as our data structure. In this case, the elements of the array are structures of a specified type. For example:

struct inventory {

int part_no;

float cost;

float price;

};

 

struct inventory table[4];

which defines an array with four elements, each of which is of type struct inventory, i.e. each is an inventory structure.

 

 

 

 

 

 

Registration


A password will be e-mailed to you.

Feedback Form

Name (required)

Email (required)

Feedback