“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 { type variable-name; type variable-name; type variable-name; : }structure variables; struct date { int day; int month int year; };

The ‘typedef ‘ Keyword.
as in
Without typdef With typedef

struct date { int day ; int month; }; struct date D1;

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; D2.day= 2; struct_name element_name

.

da y

month

year D1.yea r

D1

D2 D2.day

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; p2=p1; typedef struct pin_no { int no; }; Pin no Pin_no n1,n2; cannot be n1.no=456010; same as phone no !

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 . Elements are refrenced outermost to innermost.  here
Struct addr { int houseno; char locality[20]; char city[20]; }; Struct employee {int name; addr address; }; Struct employee shrey;

shrey.address.houseno=3;

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 to which would give complete employee ID , implement the case Study 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

1013

salary next

rahul 21,000

1053 akshay 24,000

1011 Martin 26,000

Representation as in memory.

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 …………………………… 2

struct personal { unsigned gender unsigned age unsigned mar_status unsigned children }emp; : 1 ; : 7 ; : 1 ; : 3 ;

Bit field
gender age Martial status children

Bit length
1 7 1 3

Range of values 0 or 1
0 to 127 (27 – 1) 0 or 1 0 to 7 (23 – 1)

Emp.gender=1; Emp.age=32;

Problems On Structures..
// as asked in various campus placements //

This Question was asked at technical exam
Struct point {int x,y;}; Void show(point p) {cout<<p.x<<“:”<<p.y<<endl; } Void main() { point U={20,10},V,W; V=U; V.X+=20; W=V; U.Y+=10; U.X+=5; W.X-=5; show(U);show(V);show (W); }

for IBM

Give the output of the following program . a) 25:20 40:10 35:10 e) 25:20 40:20 35:10 i) 25:10 40:10 35:10

Solution
A)
Struct point {int x,y;}; Void show(point p) {cout<<p.x<<“:”<<p.y<<endl; } Void main() { point U={20,10},V,W; V=U; V.X+=20; W=V; U.Y+=10; U.X+=5; W.X-=5; show(U);show(V);show (W); } ;

25:20 40:10 35:10

X

Y

U

V

W

Aptitude test Identify error(s) , if any in the following code snippet

struct { short day; short month; short year ; } bdate,joindate;

a) Struct tag missing 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 !

Sign up to vote on this title
UsefulNot useful