Plus Two Computer Science · Chapter 01

Structures and Pointers

7 sub-topics
12 review questions
1.1

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).

struct student
{
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
  • It is a derived data type.
  • A collection of same type of data.
  • Elements of an array are referenced using the corresponding subscripts.
  • When an element of an array becomes another array, multi-dimensional array is formed.
  • Array of structures is possible.
  • It is a user-defined data type
  • A collection of different types of data.
  • Elements of structure are referenced using dot operator ( . )
  • When an element of a structure becomes another structure, nested structure is formed.
  • Structure can contain arrays as elements

1.2

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;


int num = 25; int *ptr = &num; // ptr now holds num's address cout << *ptr; // dereference → prints 25
1.3

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.

int *p = new int(5); // allocate + initialise at runtime
delete p; // release it before it's forgotten
1.4

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.

1.5

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.

int *pass = new float[n]; // size decided at runtime
1.6

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.

1.7

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.

struct employee
{
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
Self-Assessment

Question & Answer Review

Tap a question to reveal the answer — a quick way to check understanding before moving to Chapter 2.

01What is a structure in C++?
A user-defined data type that groups logically related data items of different types under a single name.
02How does a structure differ from an array?
An array holds multiple elements of the same type, accessed by subscript. A structure holds elements of different types, accessed with the dot operator.
03What is a pointer?
A variable that stores the memory address of another variable (its L-value), rather than storing a value directly.
04What do the & and * operators do?
& (address-of) returns a variable's address. * (dereference / "value at") returns the value stored at the address a pointer holds. & works on any variable; * only makes sense on a pointer.
05What's the difference between static and dynamic memory allocation?
Static allocation happens automatically when the program loads, based on variable declarations, and its size is fixed. Dynamic allocation happens during execution via new, and must be released manually with delete.
06What is a memory leak, and how is it avoided?
Memory allocated with new but never released with delete becomes an orphaned block the program can no longer use — repeated over many runs, this is a memory leak. It's avoided by ensuring every new has a matching delete.
07Why doesn't adding 1 to a pointer just add 1 byte to its address?
Pointer arithmetic scales by the size of the pointed-to type, so ptr + 1 moves to the next element of that type — 2 bytes ahead for a short*, 4 bytes ahead for a float*, and so on.
08How is an array name related to a pointer?
An array's name evaluates to the base address of its first element, so it behaves like a pointer — except it's a constant: it can be dereferenced and indexed, but never reassigned or incremented.
09What problem does a dynamic array solve?
An ordinary array's size must be a fixed constant known at compile time, which can waste memory or fall short. A dynamic array (new type[size]) lets the size be decided while the program is running.
10Why use a character pointer instead of a character array for strings?
It avoids committing to a fixed size, allows the whole string to be reassigned with plain =, and gives faster character-by-character access through pointer arithmetic.
11How are structure members accessed through a pointer?
With the arrow operator (->) instead of the dot operator — for example, eptr->salary instead of emp.salary.
12What is a self-referential structure, and why does it matter?
A structure with a member that points to another instance of the same structure type. It's the mechanism behind dynamic data structures like linked lists, where each record points to the next.