<< Chapter < Page Chapter >> Page >

Introduction

A structure type can contain a number of dissimilar data objects within it. Unlike a simple variable (which contains only one data object) or an array (which, although it contains more than one data item, only contains items of a single data type),a structure is a collection of related data of different types. a name, for example, might be array of characters, an age might be integer. A structure representing a person, say, could contain both a name and an age, each represented in the appropriate format.

Declarations and usage of structures

Until now, all the data that we have dealt with has been either of a basic type such as char, int and double…, or an array of those types. However, there are many situations in real life where a data item needs to be made up from other more basic types. We could do this with an array if the constituent types were all the same, but often they are different. For example, suppose we want to record the details of each student in a class. The detail of each student might be as follow:

  • A unique student number, which could be represented as a string (an array of char ).
  • The student’s name, which could be represented as a string (an array of char ).
  • Final mark for the Introduction to computer science course, which is a floating point value (a float ).

Creating structures as new data types

The definition of a structure type begins with the keyword struct, and contains a list of declarations of the structure's members, in braces:

struct structTag {<list of members>; };
Definition ends with semicolon (;)

The three components above can be placed in a structure declared like this:

struct Student {char StudentID[10];char name[30];float markCS ; };
Got questions? Get instant answers now!

The keyword struct introduces a structure declaration, which is a list of declarations enclosed in braces. An optional name called a structure tag may follow the word struct (as with Student here). The tag names this kind of structure, and can be used subsequently as a shorthand for the part of the declaration in braces. The variables named in a structure are called members. A structure member or tag and an ordinary (i.e., non-member) variable can have the same name without conflict, since they can always be distinguished by context. Furthermore, the same member names may occur in different structures, although as a matter of style one would normally use the same names only for closely related objects.

Creating variable of a struct type

Structure types are not considered a variable declaration, just definition of a new type, so they cannot store anything until we declare variable of this type. Here is how we would create:

type_name_of_struct name_of_variable;

Creating three variables a, b, c of the Student type:

Student a, b, c;

Creating an array of the Student type:

Student studentCS[50];
Got questions? Get instant answers now!

A member of a structure may have any desired complete type, including previously defined structure types. They must not be variable-length arrays, or pointers to such arrays. For instance, now we want to record more information of students, for example their date of birth, which comprises the day, month and year. So first, let's start with the date, because that is a new type that we may be able to use in a variety of situations. We can declare a new type for a Date thus:

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Introduction to computer science. OpenStax CNX. Jul 29, 2009 Download for free at http://cnx.org/content/col10776/1.1
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Introduction to computer science' conversation and receive update notifications?

Ask