<< Chapter < Page Chapter >> Page >


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:

Questions & Answers

what is variations in raman spectra for nanomaterials
Jyoti Reply
I only see partial conversation and what's the question here!
Crow Reply
what about nanotechnology for water purification
RAW Reply
please someone correct me if I'm wrong but I think one can use nanoparticles, specially silver nanoparticles for water treatment.
yes that's correct
I think
what is the stm
Brian Reply
is there industrial application of fullrenes. What is the method to prepare fullrene on large scale.?
industrial application...? mmm I think on the medical side as drug carrier, but you should go deeper on your research, I may be wrong
How we are making nano material?
what is a peer
What is meant by 'nano scale'?
What is STMs full form?
scanning tunneling microscope
how nano science is used for hydrophobicity
Do u think that Graphene and Fullrene fiber can be used to make Air Plane body structure the lightest and strongest. Rafiq
what is differents between GO and RGO?
what is simplest way to understand the applications of nano robots used to detect the cancer affected cell of human body.? How this robot is carried to required site of body cell.? what will be the carrier material and how can be detected that correct delivery of drug is done Rafiq
what is Nano technology ?
Bob Reply
write examples of Nano molecule?
The nanotechnology is as new science, to scale nanometric
nanotechnology is the study, desing, synthesis, manipulation and application of materials and functional systems through control of matter at nanoscale
Is there any normative that regulates the use of silver nanoparticles?
Damian Reply
what king of growth are you checking .?
What fields keep nano created devices from performing or assimulating ? Magnetic fields ? Are do they assimilate ?
Stoney Reply
why we need to study biomolecules, molecular biology in nanotechnology?
Adin Reply
yes I'm doing my masters in nanotechnology, we are being studying all these domains as well..
what school?
biomolecules are e building blocks of every organics and inorganic materials.
anyone know any internet site where one can find nanotechnology papers?
Damian Reply
sciencedirect big data base
Introduction about quantum dots in nanotechnology
Praveena Reply
what does nano mean?
Anassong Reply
nano basically means 10^(-9). nanometer is a unit to measure length.
do you think it's worthwhile in the long term to study the effects and possibilities of nanotechnology on viral treatment?
Damian Reply
absolutely yes
how did you get the value of 2000N.What calculations are needed to arrive at it
Smarajit Reply
Privacy Information Security Software Version 1.1a
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get the best Algebra and trigonometry course in your pocket!

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?