This page is optimized for mobile devices, if you would prefer the desktop version just click here

2.6 Structures  (Page 2/4)

struct Date {int day; int month;int year; };

We can now use this Date type, together with other types, as members of a Student type which we can declare as follows:

struct Student {char studentID[10];char name[30];float markCS ; Date dateOfBirth;};

Or

struct Student {char studentID[10];char name[30];float markCS; struct Date {int day; int month;int year; } dateOfBirth;};

We can also declare structured variables when we define the structure itself:

struct Student {char studentID[10];char name[30];float markCS ; Date dateOfBirth;} a, b, c;

C permits to declare untagged structures that enable us to declare structure variables without defining a name for their structures. For example, the following structure definition declares the variables a, b, c but omits the name of the structure:

struct {char studentID[10];char name[30];float markCS ; Date dateOfBirth;} a, b, c;

A structure type cannot contain itself as a member, as its definition is not complete until the closing brace (}). However, structure types can and often do contain pointers to their own type. Such self-referential structures are used in implementing linked lists and binary trees, for example. The following example defines a type for the members of a singly linked list:

struct List { struct Student stu; // This record's data.struct List *pNext; // A pointer to the next student. };

Referencing structure members with the dot operator

Whenever we need to refer to the members of a structure, we normally use the dot operator.

For example, if we wanted to access the number member of newStudent we could do so as follows:

newStudent.studentID

We can then access the member as if it were a normal variable. For instance, we can write to this member as follows.

newStudent.studentID= “C0681008”;

We can also read from the member in a similar fashion.

printf("Student identification: %s", newStudent.studentID);

The following code outputs the contents of an Student structure.

printf("Student Details\n"); printf("Identification: %s\n", newStudent.studentID);printf("Name: %s\n", newStudent.name); printf("Mark: %.2f\n", newStudent.markCS);printf("Date of Birth: %i/%i/%i\n", newStudent.dateOfBirth.day,newStudent.dateOfBirth.month, newStudent.dateOfBirth.year);

Suppose we wish to input the details of this employee from a user. We could do so as follows.

Student newStudent; printf("Enter student identification: ");scanf("%s",&newStudent.studentID); printf("Enter student name: ");fflush(stdin);gets(newStudent.name); printf("Enter mark for Introduction to computer science course: ”);scanf("%f",&newStudent.markCS); printf("Enter birth date (dd/mm/yyyy): ");scanf("%i/%i/%i",&newStudent.dateOfBirth.day,&newStudent.dateOfBirth.month,&newStudent.dateOfBirth.year );

Initializing structure variables

When we declare a new variable of a basic data type we can initialize its value at declaration. We can also initialize structure variables at declaration as shown below.

<< Chapter < Page Page > Chapter >>

Read also:

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.
Jobilize.com uses cookies to ensure that you get the best experience. By continuing to use Jobilize.com web-site, you agree to the Terms of Use and Privacy Policy.