Chapter 1
Chapter 1
1.1 Arrays
An array is a collection of elements of the same data type, stored contiguously in memory. It's a
fundamental data structure used to organize and manipulate multiple values efficiently.
To declare an array, you specify its type, name, and the number of elements it will hold:
C++
data_type array_name[size];
C++
int numbers[5] = {10, 20, 30, 40, 50};
Accessing Elements
You access individual elements of an array using their index, which starts from 0:
C++
int first_element = numbers[0]; // Accesses the first element (10)
C++
#include <iostream>
int main() {
int numbers[5] = {10, 20, 30, 40, 50};
int sum = 0;
Key Points:
Additional Notes:
Arrays are fixed-size; you cannot change their size after declaration.
If you need a variable-sized collection, consider using dynamic arrays (vectors) from the
C++ Standard Template Library (STL).
Be careful to avoid accessing array elements out of bounds (index values outside the
valid range).
1.2 Structures
A structure is a user-defined data type that groups related variables of different data types under
a single name. It provides a way to organize and manage data efficiently.
Creating a Structure
To define a structure, you use the struct keyword followed by the structure name and a pair of
curly braces containing the member variables:
struct Student {
int roll_number;
char name[50];
float marks;
};
Once you've defined a structure, you can create variables of that type:
Accessing Members
You can access the members of a structure using the dot operator (.):
student1.roll_number = 1;
strcpy(student1.name, "Alice");
student1.marks = 95.5;
C++
#include <iostream>
#include <cstring>
struct Student {
int roll_number;
char name[50];
float marks;
};
int main() {
Student student1, student2;
student1.roll_number = 1;
strcpy(student1.name, "Alice");
student1.marks = 95.5;
student2.roll_number = 2;
strcpy(student2.name, "Bob");
student2.marks = 88.2;
return 0;
}
Key Points:
Structures allow you to group related variables into a single unit.
Each member of a structure has its own data type.
You can access members using the dot operator.
Structures can be used to represent complex data structures.
Additional Notes:
You can create arrays of structures to store multiple instances of the same data type.
Structures can be passed as arguments to functions and returned from functions.
You can use pointers to structures to manipulate them efficiently.
1.3 Functions
A function is a block of code that performs a specific task. It's a fundamental building block of
C++ programs, allowing you to modularize your code, improve readability, and reuse code.
Defining a Function
return_type function_name(parameter_list) {
// Function body
}
return_type: The data type of the value returned by the function. If the function doesn't
return a value, use void.
function_name: The name you give to the function.
parameter_list: A list of variables that the function accepts as input. Each parameter
has its data type and name.
#include <iostream>
int main() {
double length, width, area;
return 0;
}
Key Points:
Functions can be defined anywhere in the program, but it's common to place them at the
beginning or in separate header files.
Functions can be called from anywhere within the program.
Functions can return a value using the return statement.
Functions can take parameters as input.
Functions can be used to break down complex problems into smaller, more manageable
tasks.
Additional Notes:
Functions can be overloaded, meaning you can have multiple functions with the same
name but different parameter lists.
Functions can be recursive, meaning they can call themselves.
Functions can be passed as arguments to other functions.
1.4 Pointers
A pointer is a variable that stores the memory address of another variable. It provides a way to
indirectly access and manipulate data stored in memory.
Declaring Pointers
To declare a pointer, you use the * operator followed by the data type of the variable it will point
to:
data_type *pointer_name;
For example:
You can assign the address of a variable to a pointer using the & operator:
int x = 10;
int *ptr = &x; // ptr now points to x
Dereferencing Pointers
To access the value stored at the memory address pointed to by a pointer, you use the * operator:
C++
#include <iostream>
int main() {
int x = 10, y = 20;
cout << "Before swapping: x = " << x << ", y = " << y << endl;
swap(&x, &y);
cout << "After swapping: x = " << x << ", y = " << y << endl;
return 0;
Pointer Arithmetic
C++
Comparison: You can compare pointers to check if they point to the same memory
location:
if (ptr == &arr[0]) {
// ptr points to the first element of the array
}
Pointer to Pointer
A pointer can also point to another pointer:
C++
int x = 10;
int *ptr1 = &x;
int **ptr2 = &ptr1; // ptr2 points to ptr1
To access the value pointed to by the original variable through the pointer to pointer, you need to
dereference twice:
C++
int y = **ptr2; // y will be 10
Pointers are essential for dynamic memory allocation using new and delete:
Complete Example
#include <iostream>
int main() {
int *ptr;
return 0;
}
Key Points:
Additional Notes:
Be careful to avoid using null pointers (pointers that don't point to anything).
Be aware of memory leaks when using dynamic memory allocation.
Pointers can be used to create linked lists, trees, and other data structures.