DS_Lecture_05.1_05.2
DS_Lecture_05.1_05.2
DS_Lecture_05.1_05.2
2. Structure
i. Definition
ii. Defining Structure in C++
iii. Declaring Variable of Structure
iv. Access Structure Member
v. Initializing Structure Variable
vi. Some Facts about Structure
vii. Nested Structure
viii. Self-referential Structure
3. Books
4. References
Structure
Definition
The array takes simple data types like int, char or double and organizes them
into a linear array of elements all of the same type.
Now, consider a record card which records name, age and salary. The name would
have to be stored as a string, the age could be int and salary could be float. As
this record is about one person, it would be best if they are all stored under one
variable.
At the moment the only way we can work with this collection of data is as separate
variables. This isn't as convenient as a single data structure using a single name and
so the C language provides structure.
As we can declare variables for compiler defined data types (example: int a, b, *c, d[50];), we can do the
same for user defined data type created using struct.
struct EmployeeRecord{ Variable a takes –
char name[5]; 5*sizeof(char)+1*sizeof(int)+1*sizeof(float)
int age; = 5*2+1*4+1*8 = 22 bytes in the memory.
float salary;
}a; 22 bytes will be distributed sequentially to the structure
members name, age, and salary.
EmployeeRecord b, *c, d[5];
c = &a
char name[5]
a int age
float salary
Main Memory
Declaring Variable of a Structure
As we can declare variables for compiler defined data types (example: int a, b, *c, d[50];), we can do the
same for user defined data type created using struct.
struct EmployeeRecord{ Each index
contains
char name[5]; 22 bytes
int age;
float salary;
d[0] d[1] d[2] d[3] d[4]
}a;
EmployeeRecord b, *c, d[5]; Each 22 bytes contains the followings
The dot (.) or combination of dash-line and greater-than sign (->) is used as operator to refer to
members of struct.
struct EmployeeRecord{ Here, variable x is of type EmployeeRecord.
char name[5]; x.age is of type int.
int age; x.salary is of type float.
float salary; x.name is of type char[5].
}; y[2].age is of type int where y[2] is of type
EmployeeRecord and represents the 3rd element.
EmployeeRecord x, y[5], *p;
p->age is of type int where p is a pointer
x.age = 22; pointing to variable x of type EmplyeeRecord.
x.salary = 1234.56; Operator (->) is used for pointer variable of
struct instead of (.).
strcpy(x.name, "Sam");
p->age can be represented as (*p).age also.
y[2].age = 22;
p = &x;
p->age = 22;
Member variables can be used in any manner appropriate for their data type.
Initializing Structure Variable
To initialize a struct variable, follow the struct variable name with an equal sign,
followed by a list of initializers enclosed in braces in sequential order of definition.
struct EmployeeRecord{
char name[5];
int age;
float salary;
};
Mushfiqur Rahman
Some Facts About Structure
No memory (as data) is allocated for defining struct.
Memory is allocated when its instances/variables are created.
Hence struct stand alone is a template… and it cannot be initialized. You need to
declare a variable of type struct.
No arithmetic or logical operation is possible on the struct variables unless defined
by the operator overloading. Example:
EmployeeRecord a, b;
Any expression like a == b or a + b etc. is not possible.
But a.age = b.age is possible as both of these are of type int.
Example 2:
Example 2: struct DateOfBirth{
dob is a variable for the structure int day, month, year;
};
DateOfBirth.
dob is declared inside structure Employee. struct Employee{
char EmpName[100];
DateOfBirth dob;
};
Self-referential Structure
Structure member cannot be instance of enclosing struct
struct Person{
char Name[30];
Person *Child;
};
strcpy(C[1].Name, "Rahim");
Person C Person
C = C[1].Child = new Person;
Name Child Name Child
strcpy(C->Name, "Karim");
Sara Ø Karim Ø
C->Child = NULL;
Books
1. http://www.cplusplus.com/doc/tutorial/pointers/
2. http://www.cplusplus.com/doc/tutorial/structures/