Classof1 logo
Fax: 1- 425- 458- 9358 | Toll free: 1- 877- 252 - 7763
Bookmark and Share
Forgot Password? Click Here
Register  |  Account

Need help with Computer Science assignment?

Get customized homework help now!

Structures

A Structure is defined as a data type to represent different types of data with a single name. The data items in a structure are called members of the structure.

The data items in a structure are called members of the structure.

Defining a structure:

A structure definition contains a keyword ‘struct’ and a user defined tag-field followed by the members of the structure within braces. General form:

Struct tag-field
{
datatype member 1;
datatype member 2;
----------------------
-----------------------
datatype member n;
};

Where
struct – keyword to define structure
tag-field –  valid C++ name
datatype -  valid datatype such as int, float etc.

Rules:

  • Tag-field is the name given to the structure.
  • Each member definition should be terminated with semicolon.
  • Since structure definition has compound statements. It should have its own   opening and closing braces.
  • The semicolon after the closing brace is a must.

Eg:

Struct student
{
int number;
int age;
char sex;
};

This defines a structure with three members namely number, age and sex of different data types. The name of the structure is student.

The members of the structure definition are not variables. So they do not occupy any memory space.

Structure declaration:

Structure declaration means defining variables to the already defined structure

We can define variables in two ways. They are

  • Variable definition along with the structure definition.
  • Variable definition using tag-field any where in the program.

General forms:

a) Struct tag-field
{
datatype member1;
datatype member2;
---------------------
-----------------------
datatype membern;
} variable1, variable 2, …..variablen;

b) tag-field variable1, variable2…..variablen;
where
variable1,variable2,…..variablen are valid C++ variable names each having n-members

Eg:

Struct student
{
int number;
int age;
char sex;
}student1, student2, student3;

The following declarations are also valid:

a)  Struct student
{
int number;
int age;
char sex;
};student student1, student2, student3;

b) Struct
{
int number;
int age;
char sex;
} student1, student2, student3;

Giving values to structure members:

Dot operator or member operator “ . “ is used to give data to the structure variables individual members. General form:

Structure_variable.membername

The variable name with a period and the member name is used like any ordinary variable.

Computer Science Homework Help
Name* :
Email* :
Country* :
Phone* :
Subject* :
Upload Homework :
Upload another homework (upto 5 uploads max.)
Due Date
Time
AM/PM
Timezone
Instructions
(Type Security Code - case sensitive)