Structures
Structure is a user-defined data type of C++ to represent a collection of logically related data items, which may be of different types, under a common name. (e.g. an admission date inside a student record).
{
int adm_no;
char name[20];
float fee;
};
Variable declaration
student s;
Variable initialisation
student s = {3452, "Vaishakh", 270.00};
cout << s.name; // dot operator accesses a member
Variable declaration and memory allocation
struct structure_tag varl, var2,.....varn
OR
structure_tag varl, var2,.....var n
The elements of structure are accessed using dot(.) operator.
Eg. s1.name
The size depends upon the definition of the structure. The total size will be the sum of size of each data type of elements in the structure.
Nested structure
An element of a structure may itself be another structure. Such a structure is known as nested structure.
Array Vs Structure
| Arrays | Structures |
|---|---|
|
|
Pointers
A pointer is a variable that stores the address of a memory location. Two operators are used in pointers: & (address-of) retrieves a variable's address, and * (dereference / "value at") retrieves the value stored at an address.
A pointer's declared type must match the type of data it points to, since that type determines how much memory is being addressed.
A pointer variable is declared as follows
data_type *pointer_variable;
Methods of Memory Allocation
Static Memory Allocation The memory allocation that takes place before the execution of the program.
Dynamic Memory Allocation The memory is allocated during the execution of the program.
Dynamic memory allocation is done using the new operator, and must be deallocated with delete when no longer needed.
Memory Leak If the memory allocated using new operator is not freed using delete, that memory is said to be an orphaned memory block - a block of memory that is left unused, but not released for further allocation.
delete p; // release it before it's forgotten
Operations on Pointers
Pointers support limited arithmetic: adding or subtracting an integer moves the pointer by that many elements, not bytes — ptr + 1 jumps ahead by the size of whatever type ptr points to, so a short* and a float* advance by different byte counts for the same "+1". No other arithmetic (multiplication, division) is valid on pointers. Among relational operators, only equality (==) and inequality (!=) are meaningful, since they simply ask whether two pointers hold the same address.
Pointer and Array
An array's name evaluates to the address of its first element — its base address — so an array name behaves almost exactly like a pointer, and elements can be reached with either subscript notation or pointer arithmetic. The key difference: an array name is a constant address and cannot itself be incremented, while an ordinary pointer can. This pointer-array relationship also solves a real limitation of arrays: a dynamic array, created with new type[size], lets the size be decided while the program is running, instead of being fixed at compile time.
Pointer and String
A character pointer can hold a string's address just as a character array can hold a string's characters — but with real advantages: no fixed size commitment, the ability to reassign an entirely new string using plain = (which an array can't do), and faster access to individual characters via pointer arithmetic. To store a whole collection of strings, C++ uses either an array of character pointers or a 2D character array.
Pointer and Structure
When a structure is accessed through a pointer rather than a variable, its members are reached with the arrow operator (->) instead of the dot operator.
A structure can contain a pointer to its own type — a self-referential structure. This is how dynamic data structures like linked lists possible: each record can point to the next one, which contains the address of next loacation.
{
int ecode;
char ename[15];
employee *ep; // points to another employee — self-referential
};
employee *eptr;
eptr= new *employee;
eptr->ecode = 657346; // arrow operator via pointer
