You are on page 1of 5

Chapter - 1

STRUCTURES AND POINTERS


Structure: It is a user defined datatype to represent a collection of logically related
items of different data types under a common name.
Note: User defined datatypes are the datatypes defined by user to represent data of
aggregate nature

Syntax to define a structure

struct structuretag
{
datatype variablename1;
datatype variablename2;
.
.
};
here struct is the keyword to define a structure.Structure tag or structure name is
an identifier and it is the new user defined data type. Variable1,variable2 etc are the
structure elements .

Example: 1) struct date


{
int dd;
int mm;
int yy;
};

2) struct student
{
int admno;
char name[20];
char group[10];
float fee;
};
work for you

1.Define a structure employee with elements empno,empname,sex,designation,salary.


2.Define a structure item with elements itemcode,itemname,price

Variable declaration and memory allocation

Syntax: structuretag var1,var2,...;

ex: student s1,s2;


here s1 and s2 are two structure variables of type student.Memory is allocated at
the time of variable declaration.s1 and s2 requires 38 bytes of memory.(admno-
4+name-20+grour-10+fee-4)

Note: Structure tag can be avoided as follows if we declare structure variable along
with the definition.But it has some limitations.

struct
{
int admno;
char name[20];
char group[10];
float fee;
}s1,s2;

Variable initialisation
Initialisation means assigning values to a variable.After declaring a
structure variable it can be initialised as follows.
Syntax: structuretag variable={value1,value2,......};
Ex: student s1={100,”Anurag”,”Science”,250.00};

here the values 100,Anurag,Science,250.00 are assigned to the elements


admno,name,group and fee respectively.

Note: A structure variable can be assigned with the values of another structure
variable if they should be of the same structure type
ex: student s2=s1;
Accessing elements of a structure
The dot operator(.) is used with structure variable for accessing elements of a
structure.
Syntax: stucturevariable.elementname;
Ex: cin>>s1.admno;
cout<<s1.fee;

Note:
Consider the two structures given below.
struct test1 struct test2
{ {
int a; and int a;
float b; float b;
}t1={4,5.5} }t2;

here t2=t1 is invalid because t2 and t1 are of different types as test2 and
test1respectively.
But t2.a=t1.a; and t2.b=t1.b is possible.
Program1: Using structure to read and write the details of a students

#include<iostream>
#include<cstdio>
using namespace std;
struct student
{
int admno;
char name[20];
char group[10];
float fee;
};
int main()
{
student s1;
cout<<”Enter admissionno:”;
cin>>s1.admno;
cout<<”Enter student name:”;
gets(s1.name);
cout<<”Enter group:”;
gets(s1.group);
cout<<”Enter fee:”;
cin>>s1.fee;
cout<<s1.admno<<” “<<s1.name<<” “<<s1.group<<” “ <<s1.fee;
return 0;
}

Note: A structure can be defined within or outside the main().If it is outside its scope
is global.otherwise local to main()

Work for you


1. Write a program to read and write the details of an employee using structure.
2.Write a program to read and write the details of an item using structure.
3.program to find the total score of a student(total=ce+te+pe)[refer your text book]

Array of structure
consider the statement
student s[10];
here s is a student array can contain the details of 10 students.
Nested structure
If a structure contains another structure type data as its element such a structure
is called a nested structure.

Ex: struct student


{
int admno;
char name[20];
struct date
{
int dd,mm,yy;
}dtadm;
float fee;
}

or struct date
{
int dd,mm,yy;
};
struct student
{
int admno;
char name[20];
date dtadm;
float fee;
};

Here student is a nested structure as it contains another structure type element dtadm
which is of type date.

Accessing elements of a nested structure


Syntax: outerstructurevariable.innerstructurevariable.elementname;
ex: student s={100,”Anurag”,{10,12,2019},600};
Here s is the outer structure variable and dtadm is the inner atructure variable.

cout<<s.admno<<s.name<<s.dtadm.dd<<s.dtadm.mm<<s.dtadm.yy<<s.fee;

Array and structure


Array Structure
1. It is a derived datatype 1. user defined datatype
2. collection of same type data 2. collection of different type data
3. subscripts are used to access elements 3. dot operator is used to access elements
4.multidimensional array is possible 4.Nested structure is possible
5. array of structures is possible 5. structure can contain array as elements

You might also like