“Algorithms + Data Structures = Programs!”
- Niklaus Wirth
STRUCTURES user defined data type. Collection of variables refrenced under one name. in ‘C++’ has backward compatibility with ‘ C’. Declared with the help of keyword ‘struct’. All members are public by default .
Structure Declaration. struct tag
struct date {
{ type variable-name; type variable-name; type variable-name; : }structure variables;
int day; int month int year; };
The ‘typedef ‘ Keyword. as in Without typdef
struct date { int day ; int month; }; struct date D1;
With typedef
typedef struct date { int day ; int month; }; date D1;
The ‘typedef ‘ Keyword. as in typedef struct date { int day ; int month; }dat; date D1; dat D2;
/* As Refrence provides alias name to a variable. typedef provides an alias name for a data type. */
Refrencing Structure elements.
.
D1.year=1988;
struct_name element_name
D2.day= 2;
da y
D1
D2 D2.day
month
year D1.yea r
Structure Assignments. One structure variable can be assigned another variable of the same structure type. even if the data members are same (in no. and in type ) we cannot assign to different structure type. Only Assignment (=) is possible for two similar structures. Other Operations like ( == ) OR ( ! = ) are not possible in most versions of compliers.
Structure Assignments. Eg. typedef struct phoneno { int no; }; phoneno p1,p2; p1.no=6491444;
typedef struct pin_no { int no; }; Pin no Pin_no n1,n2; cannot be n1.no=456010; same as phone no !
p2=p1;
n2=n1; p2=n2;
//error: type
Complex Structures Structure elements may be simple or complex.
Simple elements are of fundamental type like int,float etc . complex elements are like arrays or even structures. such structures are called COMPLEX Structures. when element is a structure, then NESTED Structure.
Structures and Arrays Array of structures. eg. Struct emp e[10]; Arrays within structure eg. Struct student { char name[20]; int marks[5]; };
Nested Structures structure within a structure inner structure should be defined before outer structure .
Struct addr { int houseno; char locality[20]; char city[20];
Elements are refrenced outermost to innermost.
};
here
{int name;
shrey.address.houseno=3;
Struct employee
addr address; }; Struct employee shrey;
Structures and functions.
Passing structure elements to Functions eg. int s = print (bday.day,bday.month); /* if one of the elements is an array , it will automatically passed as by refrence as arrays cant be passed by value! */
Structures and functions. Passing whole structures eg. Void prnsum(struct distance d1,struct distance d2) ;
returning whole structures eg. Distance prnsum ( distance d1, distance d2 ) { distance d3; d3.feet=d1.feet+d2.feet; return d3; }
A Case Study ! A company has a no. of employees. A person’s (probably HR manager) job is to store information about all the new employees. The information includes Name, Age, Address and Basic Salary. Whenever the manager wants to retrieve any information he must punch in the Program the case Studycomplete employee IDto, implement which would give information of that particular employee.
Self Referential Structures.. as in A Structure having a member element that refers to the structure itself , is called a self-referential Structure.
mainly used for creation of Linked Lists. has an additional pointer that points to the structure. eg. struct employee { int empno; char name[20]; employee *next; };
Linked Lists using self referential Structures
empn o name
salary next
1013
rahul 21,000
1053 akshay 24,000
Representation as in memory.
1011 Martin 26,000
The Concept of BIT FIELDS A bit field is a set of adjacent bits whose size varies from 1-16 bits in length. General form of bit field definition : struct tag name { data-type name1 : bit-length; data-type name2 : bit length ; ……. data type nameN : bit length; };
Data type is either int or unsigned int, bit length is the no. of bits. ‘scanf’ of values is not possible !
The Concept of BIT FIELDS 15 1
14 0
13 12
struct personal
……………………………
2
Bit field
Bit length
gender
1
age
7
0 to 127 (27 – 1)
1
0 or 1
Emp.gender=1;
Martial status
Emp.age=32;
children
3
0 to 7 (23 – 1)
{ unsigned gender
: 1 ;
unsigned age
: 7 ;
unsigned mar_status
: 1 ;
unsigned children
: 3 ;
}emp;
Range of values 0 or 1
Problems On Structures.. // as asked in various campus placements //
This Question was asked at technical exam
for IBM
Give the output of the following program . Struct point {int x,y;}; Void show(point p) {cout<
a) 25:20 40:10 35:10
Void main() { point U={20,10},V,W; V=U;
e) 25:20 40:20
V.X+=20;
35:10
W=V; U.Y+=10; U.X+=5;
i) 25:10
W.X-=5; show(U);show(V);show (W); }
40:10 35:10
Solution A) Struct point {int x,y;};
25:20 40:10 35:10
X
Void show(point p) {cout<
U
{ point U={20,10},V,W; V=U; V.X+=20;
V
W=V; U.Y+=10; U.X+=5; W.X-=5; show(U);show(V);show (W); } ;
W
Y
Aptitude test Identify error(s) , if any in the following code snippet
a) Struct tag missing
struct { short day; short month; short year ; } bdate,joindate;
b) more than 1 variable declared. c) terminated incorrectly d) NO error !
Solution Correct Ans is D) NO error !
When Structure tag is missing , it is possible to access the structure tag through the variables created just after ! struct date
{ short day; short month; short year ; }bdate,joindate; is also correct
Possible Questions for Technical interview. Use of Typedef in C / Use of Typedef in C++ Difference between structure and arrays Difference between structure and union. Can a Structure contain a Pointer to itself? How are Structure passing and returning implemented by the complier? What are bit fields? What is the use of bit fields in a Structure declaration? How would you use qsort() function to sort an array of structures? How can we read/write Structures from/to data files?
THANK YOU !