Unit 3: Arrays
ARRAYS IN C
An array is a collection of multiple values of the same data type, stored in contiguous (continuous) memory locations.
It allows storing many values under one variable name.
Example
This creates space for 5 integers.
Array Notation and Representation
Every array has:
- Name: identifier
- Index: starts from 0
- Elements: values stored inside
- Size: number of elements
Example
Index mapping:
| Index | Value |
|---|---|
| 0 | 10 |
| 1 | 20 |
| 2 | 30 |
| 3 | 40 |
| 4 | 50 |
Memory is stored continuously.
Declaring a One-Dimensional Array
Syntax
Example
Size must be a positive integer.
Initializing Arrays
You can assign initial values at the time of declaration.
A. Full Initialization
B. Partial Initialization
Remaining values become 0.
C. Without specifying size
Compiler determines size.
D. String Initialization
Char arrays behave like strings.
Accessing Array Elements
Use the index number.
Example
Manipulating Array Elements
You can update, modify, or process array values using loops.
Example: Adding 5 to every element
Example: Searching for an element
Arrays of Unknown or Varying Size
A. Using user input
Size is given by user.
B. Using dynamic memory (malloc)
For very large or flexible arrays.
Two-Dimensional Arrays (2D Arrays)
2D arrays are like tables with rows and columns.
Syntax
Example
Initialization
Accessing Elements
Multidimensional Arrays
Arrays with more than 2 dimensions (3D, 4D...).
Example of 3D Array
This means:
- 2 tables
- each table has 3 rows
- each row has 4 columns
Accessing elements
Difference Between 1D, 2D & Multi-D Arrays
| Type | Structure | Example |
|---|---|---|
| 1D | Single row | int a[5] |
| 2D | Table (rows × columns) | int b[3][3] |
| 3D & above | Multiple tables | int c[2][3][4] |
Real-Life Uses of Arrays
| Area | Use |
|---|---|
| Data science | Storing datasets |
| Matrices | Mathematical operations |
| Image processing | Pixel representation (2D/3D arrays) |
| Games | Storing maps, scores |
| Banking | Customer records |
Example Program (2D Array Sum)
SUMMARY TABLE
| Concept | Explanation |
|---|---|
| Array | Collection of similar data |
| 1D Array | Linear structure |
| 2D Array | Row-column matrix |
| Initialization | Assigning values |
| Accessing | Using index |
| Manipulation | Updating array values |
| VLA | Variable Length Arrays |
| Multidimensional | 3D, 4D… arrays |
POINTERS IN C
Introduction to Pointers
A pointer is a variable that stores the memory address of another variable.
Example:
Here:
a→ stores value 10p→ stores address of a*p→ gives value ofa(dereferencing)
Why pointers are important?
- Memory management
- Dynamic allocation
- Call by reference
- Efficient array handling
- Structures, linked lists, trees
- Function pointers
Characteristics of Pointers
- Pointers store address, not value
- They consume fixed memory (usually 4 or 8 bytes)
- Pointer type must match the variable type
- Pointer arithmetic is possible (+, -, ++, --)
- Pointers are powerful but require careful usage
& and * Operators
& (Address-of operator)
Used to get the address of a variable.
* (Dereference operator)
Used to access the value stored at the address.
Pointer Type Declaration and Assignment
Declaration
Assignment
Pointers must be assigned addresses, not values.
Pointer Arithmetic
Valid operations for pointers:
| Operation | Meaning |
|---|---|
p++ | Move to next memory location |
p-- | Move to previous location |
p + n | Jump forward n elements |
p - n | Move backward n elements |
Example
Pointer arithmetic jumps based on data type size.
For int (4 bytes):
-
p+1→ next 4-byte block
Call By Reference Using Pointers
C does not support call-by-reference directly.
But we can simulate it using pointers.
Example
Passing Pointers to Functions
Pointers allow functions to modify actual variables.
Example
Calling:
Array and Pointers
Arrays and pointers are closely related.
Example
p[0] = 1- *(p+2) = 3
Array of Pointers
An array that stores addresses.
Example: array of strings
Example: array of integer pointers
Pointer to Pointer (Double Pointer)
A pointer that stores the address of another pointer.
Syntax
Example
Pointer to Function
A pointer that stores the address of a function.
Syntax
Example
Uses:
- Callbacks
- Event handling
- Dynamic function selection
Array of Function Pointers
Stores multiple function addresses.
Example
Summary Table
| Concept | Meaning |
|---|---|
| Pointer | Stores address |
& | Address-of |
* | Value-at-address |
| Pointer arithmetic | Move to next/previous element |
| Passing pointer | Allows modification in functions |
| Array of pointers | Stores multiple addresses |
| Pointer to pointer | Double-level addressing |
| Function pointer | Holds function address |
STRINGS IN C
A string in C is a collection of characters stored in a character array and terminated with a null character \0.
Example:
Internally stored as:
| I | n | d | i | a | \0 |
Important:
C does not have a built-in "string" data type.
Strings are handled using character arrays or char pointers.
Initializing Strings
A. Initialization using character array
B. Initialization using string literal (easier)
C. Initialize an empty string
D. Without specifying size
Compiler decides size automatically.
Accessing String Elements
You use index numbers (0,1,2…) like with arrays.
Example:
You can also modify characters:
Array of Strings
An array of strings means multiple strings stored together.
Method 1: Using 2D array
Memory representation:
| Delhi | Mumbai | Goa |
Method 2: Using array of char pointers
(More memory efficient)
Passing Strings to Functions
Strings are passed as character pointers because arrays decay to pointers.
Example:
You can also use:
Common String Functions in C (string.h)
Include header:
1. strlen() — length of string
2. strcpy() — copy string
3. strcat() — concatenate two strings
4. strcmp() — compare two strings
Returns:
0→ strings are equal<0→ str1 < str2>0→ str1 > str2
5. strrev() — reverse a string
(Not available in all compilers)
6. strlwr() / strupr() — lowercase / uppercase
Compiler dependent.
Example Program: Count Vowels in a String
Example Program: Reverse a String Using Function
Summary Table
| Topic | Explanation |
|---|---|
| String | Character array ending with \0 |
| Initialization | Literal or char array |
| Accessing | Using index |
| Array of strings | 2D array or pointer array |
| Passing to function | Passed as pointer |
| String functions | strlen, strcpy, strcat, strcmp, strrev |