<< Chapter < Page Chapter >> Page >
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.

Questions & Answers

what is mutation
Janga Reply
what is a cell
Sifune Reply
how is urine form
Sifune
what is antagonism?
mahase Reply
classification of plants, gymnosperm features.
Linsy Reply
what is the features of gymnosperm
Linsy
how many types of solid did we have
Samuel Reply
what is an ionic bond
Samuel
What is Atoms
Daprince Reply
what is fallopian tube
Merolyn
what is bladder
Merolyn
what's bulbourethral gland
Eduek Reply
urine is formed in the nephron of the renal medulla in the kidney. It starts from filtration, then selective reabsorption and finally secretion
onuoha Reply
State the evolution relation and relevance between endoplasmic reticulum and cytoskeleton as it relates to cell.
Jeremiah
what is heart
Konadu Reply
how is urine formed in human
Konadu
how is urine formed in human
Rahma
what is the diference between a cavity and a canal
Pelagie Reply
what is the causative agent of malaria
Diamond
malaria is caused by an insect called mosquito.
Naomi
Malaria is cause by female anopheles mosquito
Isaac
Malaria is caused by plasmodium Female anopheles mosquitoe is d carrier
Olalekan
a canal is more needed in a root but a cavity is a bad effect
Commander
what are pathogens
Don Reply
In biology, a pathogen (Greek: πάθος pathos "suffering", "passion" and -γενής -genēs "producer of") in the oldest and broadest sense, is anything that can produce disease. A pathogen may also be referred to as an infectious agent, or simply a germ. The term pathogen came into use in the 1880s.[1][2
Zainab
A virus
Commander
Definition of respiration
Muhsin Reply
respiration is the process in which we breath in oxygen and breath out carbon dioxide
Achor
how are lungs work
Commander
where does digestion begins
Achiri Reply
in the mouth
EZEKIEL
what are the functions of follicle stimulating harmones?
Rashima Reply
stimulates the follicle to release the mature ovum into the oviduct
Davonte
what are the functions of Endocrine and pituitary gland
Chinaza
endocrine secrete hormone and regulate body process
Achor
while pituitary gland is an example of endocrine system and it's found in the Brain
Achor
what's biology?
Egbodo Reply
Biology is the study of living organisms, divided into many specialized field that cover their morphology, physiology,anatomy, behaviour,origin and distribution.
Lisah
biology is the study of life.
Alfreda
Biology is the study of how living organisms live and survive in a specific environment
Sifune
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

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