Arrays and Pointers in C Explained
Arrays and Pointers in C Explained
INTRODUCTION TO ARRAYS
An array is a collection of elements of the same data type stored in contiguous memory
locations.
Arrays organize similar data efficiently and support fast access using indexes.
Characteristics:
1. Fixed size
2. Same data type
3. Contiguous memory
4. Random access capability
Syntax:
data_type array_name[size];
Example:
int marks[5];
Types of Arrays:
1. One-dimensional array
2. Two-dimensional array
3. Multi-dimensional array
-------------------------------------------------------------
ONE DIMENSIONAL ARRAY
-------------------------------------------------------------
Example:
int arr[5] = {10,20,30,40,50};
Memory Representation:
Index: 0 1 2 3 4
Value: 10 20 30 40 50
-------------------------------------------------------------
TWO DIMENSIONAL ARRAY
-------------------------------------------------------------
Stores data in rows and columns.
Syntax:
data_type arr[row][col];
Example:
int mat[2][3] = {{1,2,3},{4,5,6}};
-------------------------------------------------------------
MULTI DIMENSIONAL ARRAY
-------------------------------------------------------------
Example:
int a[3][3][2];
-------------------------------------------------------------
ARRAY PASSING TO FUNCTION
-------------------------------------------------------------
Arrays are passed by reference.
Example:
void display(int arr[]) { ... }
-------------------------------------------------------------
INTRODUCTION TO POINTERS
-------------------------------------------------------------
A pointer stores the address of another variable.
Syntax:
data_type *ptr;
Example:
int a = 10;
int *p = &a;
-------------------------------------------------------------
ADDRESS AND DEREFERENCING
-------------------------------------------------------------
& gives address
* accesses value at address
Example:
printf("%d", *p);
-------------------------------------------------------------
POINTER ARITHMETIC
-------------------------------------------------------------
Allowed:
p++, p--, p+n, p-n
-------------------------------------------------------------
POINTER TO POINTER
-------------------------------------------------------------
int a = 10;
int *p = &a;
int **q = &p;
-------------------------------------------------------------
POINTERS AND ARRAYS
-------------------------------------------------------------
Array name behaves as pointer to first element.
Example:
int a[5] = {1,2,3,4,5};
int *p = a;
-------------------------------------------------------------
ARRAY OF POINTERS
-------------------------------------------------------------
int *p[3];
-------------------------------------------------------------
POINTER TO ARRAY
-------------------------------------------------------------
int (*p)[5];
-------------------------------------------------------------
DYNAMIC MEMORY ALLOCATION
-------------------------------------------------------------
Functions:
malloc(), calloc(), realloc(), free()
Example:
int *p = (int*)malloc(5*sizeof(int));
-------------------------------------------------------------
ARRAY & POINTER RELATIONSHIP
-------------------------------------------------------------
- Array name = constant pointer
- Pointer can traverse array
- Array size fixed, pointer flexible
-------------------------------------------------------------
PRACTICE PROGRAMS
-------------------------------------------------------------
1. Reverse array using pointer
2. Sum array using pointer
3. Matrix operations
4. Pointer-to-pointer demo
-------------------------------------------------------------
SUMMARY
-------------------------------------------------------------
Arrays store data sequentially; pointers access memory directly.
Both are essential for memory-efficient programming in C.
-------------------------------------------------------------
EXTRA EXPLANATION...
INTRODUCTION TO ARRAYS
An array is a collection of elements of the same data type stored in contiguous memory
locations.
Arrays organize similar data efficiently and support fast access using indexes.
Characteristics:
1. Fixed size
2. Same data type
3. Contiguous memory
4. Random access capability
Syntax:
data_type array_name[size];
Example:
int marks[5];
Types of Arrays:
1. One-dimensional array
2. Two-dimensional array
3. Multi-dimensional array
-------------------------------------------------------------
ONE DIMENSIONAL ARRAY
-------------------------------------------------------------
Example:
int arr[5] = {10,20,30,40,50};
Memory Representation:
Index: 0 1 2 3 4
Value: 10 20 30 40 50
-------------------------------------------------------------
TWO DIMENSIONAL ARRAY
-------------------------------------------------------------
Stores data in rows and columns.
Syntax:
data_type arr[row][col];
Example:
int mat[2][3] = {{1,2,3},{4,5,6}};
-------------------------------------------------------------
MULTI DIMENSIONAL ARRAY
-------------------------------------------------------------
Example:
int a[3][3][2];
-------------------------------------------------------------
ARRAY PASSING TO FUNCTION
-------------------------------------------------------------
Arrays are passed by reference.
Example:
void display(int arr[]) { ... }
-------------------------------------------------------------
INTRODUCTION TO POINTERS
-------------------------------------------------------------
A pointer stores the address of another variable.
Syntax:
data_type *ptr;
Example:
int a = 10;
int *p = &a;
-------------------------------------------------------------
ADDRESS AND DEREFERENCING
-------------------------------------------------------------
& gives address
* accesses value at address
Example:
printf("%d", *p);
-------------------------------------------------------------
POINTER ARITHMETIC
-------------------------------------------------------------
Allowed:
p++, p--, p+n, p-n
-------------------------------------------------------------
POINTER TO POINTER
-------------------------------------------------------------
int a = 10;
int *p = &a;
int **q = &p;
-------------------------------------------------------------
POINTERS AND ARRAYS
-------------------------------------------------------------
Array name behaves as pointer to first element.
Example:
int a[5] = {1,2,3,4,5};
int *p = a;
-------------------------------------------------------------
ARRAY OF POINTERS
-------------------------------------------------------------
int *p[3];
-------------------------------------------------------------
POINTER TO ARRAY
-------------------------------------------------------------
int (*p)[5];
-------------------------------------------------------------
DYNAMIC MEMORY ALLOCATION
-------------------------------------------------------------
Functions:
malloc(), calloc(), realloc(), free()
Example:
int *p = (int*)malloc(5*sizeof(int));
-------------------------------------------------------------
ARRAY & POINTER RELATIONSHIP
-------------------------------------------------------------
- Array name = constant pointer
- Pointer can traverse array
- Array size fixed, pointer flexible
-------------------------------------------------------------
PRACTICE PROGRAMS
-------------------------------------------------------------
1. Reverse array using pointer
2. Sum array using pointer
3. Matrix operations
4. Pointer-to-pointer demo
-------------------------------------------------------------
SUMMARY
-------------------------------------------------------------
Arrays store data sequentially; pointers access memory directly.
Both are essential for memory-efficient programming in C.
-------------------------------------------------------------
EXTRA EXPLANATION...
INTRODUCTION TO ARRAYS
An array is a collection of elements of the same data type stored in contiguous memory
locations.
Arrays organize similar data efficiently and support fast access using indexes.
Characteristics:
1. Fixed size
2. Same data type
3. Contiguous memory
4. Random access capability
Syntax:
data_type array_name[size];
Example:
int marks[5];
Types of Arrays:
1. One-dimensional array
2. Two-dimensional array
3. Multi-dimensional array
-------------------------------------------------------------
ONE DIMENSIONAL ARRAY
-------------------------------------------------------------
Example:
int arr[5] = {10,20,30,40,50};
Memory Representation:
Index: 0 1 2 3 4
Value: 10 20 30 40 50
-------------------------------------------------------------
TWO DIMENSIONAL ARRAY
-------------------------------------------------------------
Stores data in rows and columns.
Syntax:
data_type arr[row][col];
Example:
int mat[2][3] = {{1,2,3},{4,5,6}};
-------------------------------------------------------------
MULTI DIMENSIONAL ARRAY
-------------------------------------------------------------
Example:
int a[3][3][2];
-------------------------------------------------------------
ARRAY PASSING TO FUNCTION
-------------------------------------------------------------
Arrays are passed by reference.
Example:
void display(int arr[]) { ... }
-------------------------------------------------------------
INTRODUCTION TO POINTERS
-------------------------------------------------------------
A pointer stores the address of another variable.
Syntax:
data_type *ptr;
Example:
int a = 10;
int *p = &a;
-------------------------------------------------------------
ADDRESS AND DEREFERENCING
-------------------------------------------------------------
& gives address
* accesses value at address
Example:
printf("%d", *p);
-------------------------------------------------------------
POINTER ARITHMETIC
-------------------------------------------------------------
Allowed:
p++, p--, p+n, p-n
-------------------------------------------------------------
POINTER TO POINTER
-------------------------------------------------------------
int a = 10;
int *p = &a;
int **q = &p;
-------------------------------------------------------------
POINTERS AND ARRAYS
-------------------------------------------------------------
Array name behaves as pointer to first element.
Example:
int a[5] = {1,2,3,4,5};
int *p = a;
-------------------------------------------------------------
ARRAY OF POINTERS
-------------------------------------------------------------
int *p[3];
-------------------------------------------------------------
POINTER TO ARRAY
-------------------------------------------------------------
int (*p)[5];
-------------------------------------------------------------
DYNAMIC MEMORY ALLOCATION
-------------------------------------------------------------
Functions:
malloc(), calloc(), realloc(), free()
Example:
int *p = (int*)malloc(5*sizeof(int));
-------------------------------------------------------------
ARRAY & POINTER RELATIONSHIP
-------------------------------------------------------------
- Array name = constant pointer
- Pointer can traverse array
- Array size fixed, pointer flexible
-------------------------------------------------------------
PRACTICE PROGRAMS
-------------------------------------------------------------
1. Reverse array using pointer
2. Sum array using pointer
3. Matrix operations
4. Pointer-to-pointer demo
-------------------------------------------------------------
SUMMARY
-------------------------------------------------------------
Arrays store data sequentially; pointers access memory directly.
Both are essential for memory-efficient programming in C.
-------------------------------------------------------------
EXTRA EXPLANATION...
INTRODUCTION TO ARRAYS
An array is a collection of elements of the same data type stored in contiguous memory
locations.
Arrays organize similar data efficiently and support fast access using indexes.
Characteristics:
1. Fixed size
2. Same data type
3. Contiguous memory
4. Random access capability
Syntax:
data_type array_name[size];
Example:
int marks[5];
Types of Arrays:
1. One-dimensional array
2. Two-dimensional array
3. Multi-dimensional array
-------------------------------------------------------------
ONE DIMENSIONAL ARRAY
-------------------------------------------------------------
Example:
int arr[5] = {10,20,30,40,50};
Memory Representation:
Index: 0 1 2 3 4
Value: 10 20 30 40 50
-------------------------------------------------------------
TWO DIMENSIONAL ARRAY
-------------------------------------------------------------
Stores data in rows and columns.
Syntax:
data_type arr[row][col];
Example:
int mat[2][3] = {{1,2,3},{4,5,6}};
-------------------------------------------------------------
ARRAY PASSING TO FUNCTION
-------------------------------------------------------------
Arrays are passed by reference.
Example:
void display(int arr[]) { ... }
-------------------------------------------------------------
INTRODUCTION TO POINTERS
-------------------------------------------------------------
A pointer stores the address of another variable.
Syntax:
data_type *ptr;
Example:
int a = 10;
int *p = &a;
-------------------------------------------------------------
ADDRESS AND DEREFERENCING
-------------------------------------------------------------
& gives address
* accesses value at address
Example:
printf("%d", *p);
-------------------------------------------------------------
POINTER ARITHMETIC
-------------------------------------------------------------
Allowed:
p++, p--, p+n, p-n
-------------------------------------------------------------
POINTERS AND ARRAYS
-------------------------------------------------------------
Array name behaves as pointer to first element.
Example:
int a[5] = {1,2,3,4,5};
int *p = a;
-------------------------------------------------------------
ARRAY OF POINTERS
-------------------------------------------------------------
int *p[3];
-------------------------------------------------------------
POINTER TO ARRAY
-------------------------------------------------------------
int (*p)[5];
-------------------------------------------------------------
DYNAMIC MEMORY ALLOCATION
-------------------------------------------------------------
Functions:
malloc(), calloc(), realloc(), free()
Example:
int *p = (int*)malloc(5*sizeof(int));
-------------------------------------------------------------
ARRAY & POINTER RELATIONSHIP
-------------------------------------------------------------
- Array name = constant pointer
- Pointer can traverse array
- Array size fixed, pointer flexible
-------------------------------------------------------------
PRACTICE PROGRAMS
-------------------------------------------------------------
1. Reverse array using pointer
2. Sum array using pointer
3. Matrix operations
4. Pointer-to-pointer demo
-------------------------------------------------------------
SUMMARY
-------------------------------------------------------------
Arrays store data sequentially; pointers access memory directly.
Both are essential for memory-efficient programming in C.
-------------------------------------------------------------
EXTRA EXPLANATION...
INTRODUCTION TO ARRAYS
An array is a collection of elements of the same data type stored in contiguous memory
locations.
Arrays organize similar data efficiently and support fast access using indexes.
Characteristics:
1. Fixed size
2. Same data type
3. Contiguous memory
4. Random access capability
Syntax:
data_type array_name[size];
Example:
int marks[5];
Types of Arrays:
1. One-dimensional array
2. Two-dimensional array
3. Multi-dimensional array
-------------------------------------------------------------
ONE DIMENSIONAL ARRAY
-------------------------------------------------------------
Example:
int arr[5] = {10,20,30,40,50};
Memory Representation:
Index: 0 1 2 3 4
Value: 10 20 30 40 50
-------------------------------------------------------------
TWO DIMENSIONAL ARRAY
-------------------------------------------------------------
Stores data in rows and columns.
Syntax:
data_type arr[row][col];
Example:
int mat[2][3] = {{1,2,3},{4,5,6}};
-------------------------------------------------------------
MULTI DIMENSIONAL ARRAY
-------------------------------------------------------------
Example:
int a[3][3][2];
-------------------------------------------------------------
ARRAY PASSING TO FUNCTION
-------------------------------------------------------------
Arrays are passed by reference.
Example:
void display(int arr[]) { ... }
-------------------------------------------------------------
INTRODUCTION TO POINTERS
-------------------------------------------------------------
A pointer stores the address of another variable.
Syntax:
data_type *ptr;
Example:
int a = 10;
int *p = &a;
-------------------------------------------------------------
ADDRESS AND DEREFERENCING
-------------------------------------------------------------
& gives address
* accesses value at address
Example:
printf("%d", *p);
-------------------------------------------------------------
POINTER ARITHMETIC
-------------------------------------------------------------
Allowed:
p++, p--, p+n, p-n
-------------------------------------------------------------
POINTER TO POINTER
-------------------------------------------------------------
int a = 10;
int *p = &a;
int **q = &p;
-------------------------------------------------------------
POINTERS AND ARRAYS
-------------------------------------------------------------
Array name behaves as pointer to first element.
Example:
int a[5] = {1,2,3,4,5};
int *p = a;
-------------------------------------------------------------
ARRAY OF POINTERS
-------------------------------------------------------------
int *p[3];
-------------------------------------------------------------
POINTER TO ARRAY
-------------------------------------------------------------
int (*p)[5];
-------------------------------------------------------------
DYNAMIC MEMORY ALLOCATION
-------------------------------------------------------------
Functions:
malloc(), calloc(), realloc(), free()
Example:
int *p = (int*)malloc(5*sizeof(int));
-------------------------------------------------------------
ARRAY & POINTER RELATIONSHIP
-------------------------------------------------------------
- Array name = constant pointer
- Pointer can traverse array
- Array size fixed, pointer flexible
-------------------------------------------------------------
PRACTICE PROGRAMS
-------------------------------------------------------------
1. Reverse array using pointer
2. Sum array using pointer
3. Matrix operations
4. Pointer-to-pointer demo
-------------------------------------------------------------
SUMMARY
-------------------------------------------------------------
Arrays store data sequentially; pointers access memory directly.
Both are essential for memory-efficient programming in C.
-------------------------------------------------------------
EXTRA EXPLANATION...
ARRAY AND POINTER – DETAILED NOTES
-------------------------------------------------------------
INTRODUCTION TO ARRAYS
An array is a collection of elements of the same data type stored in contiguous memory
locations.
Arrays organize similar data efficiently and support fast access using indexes.
Characteristics:
1. Fixed size
2. Same data type
3. Contiguous memory
4. Random access capability
Syntax:
data_type array_name[size];
Example:
int marks[5];
Types of Arrays:
1. One-dimensional array
2. Two-dimensional array
3. Multi-dimensional array
-------------------------------------------------------------
ONE DIMENSIONAL ARRAY
-------------------------------------------------------------
Example:
int arr[5] = {10,20,30,40,50};
Memory Representation:
Index: 0 1 2 3 4
Value: 10 20 30 40 50
-------------------------------------------------------------
TWO DIMENSIONAL ARRAY
-------------------------------------------------------------
Stores data in rows and columns.
Syntax:
data_type arr[row][col];
Example:
int mat[2][3] = {{1,2,3},{4,5,6}};
-------------------------------------------------------------
MULTI DIMENSIONAL ARRAY
-------------------------------------------------------------
Example:
int a[3][3][2];
-------------------------------------------------------------
ARRAY PASSING TO FUNCTION
-------------------------------------------------------------
Arrays are passed by reference.
Example:
void display(int arr[]) { ... }
-------------------------------------------------------------
INTRODUCTION TO POINTERS
-------------------------------------------------------------
A pointer stores the address of another variable.
Syntax:
data_type *ptr;
Example:
int a = 10;
int *p = &a;
-------------------------------------------------------------
ADDRESS AND DEREFERENCING
-------------------------------------------------------------
& gives address
* accesses value at address
Example:
printf("%d", *p);
-------------------------------------------------------------
POINTER ARITHMETIC
-------------------------------------------------------------
Allowed:
p++, p--, p+n, p-n
-------------------------------------------------------------
POINTER TO POINTER
-------------------------------------------------------------
int a = 10;
int *p = &a;
int **q = &p;
-------------------------------------------------------------
POINTERS AND ARRAYS
-------------------------------------------------------------
Array name behaves as pointer to first element.
Example:
int a[5] = {1,2,3,4,5};
int *p = a;
-------------------------------------------------------------
ARRAY OF POINTERS
-------------------------------------------------------------
int *p[3];
-------------------------------------------------------------
POINTER TO ARRAY
-------------------------------------------------------------
int (*p)[5];
-------------------------------------------------------------
DYNAMIC MEMORY ALLOCATION
-------------------------------------------------------------
Functions:
malloc(), calloc(), realloc(), free()
Example:
int *p = (int*)malloc(5*sizeof(int));
-------------------------------------------------------------
ARRAY & POINTER RELATIONSHIP
-------------------------------------------------------------
- Array name = constant pointer
- Pointer can traverse array
- Array size fixed, pointer flexible
-------------------------------------------------------------
PRACTICE PROGRAMS
-------------------------------------------------------------
1. Reverse array using pointer
2. Sum array using pointer
3. Matrix operations
4. Pointer-to-pointer demo
-------------------------------------------------------------
SUMMARY
-------------------------------------------------------------
Arrays store data sequentially; pointers access memory directly.
Both are essential for memory-efficient programming in C.
-------------------------------------------------------------
EXTRA EXPLANATION...
INTRODUCTION TO ARRAYS
An array is a collection of elements of the same data type stored in contiguous memory
locations.
Arrays organize similar data efficiently and support fast access using indexes.
Characteristics:
1. Fixed size
2. Same data type
3. Contiguous memory
4. Random access capability
Syntax:
data_type array_name[size];
Example:
int marks[5];
Types of Arrays:
1. One-dimensional array
2. Two-dimensional array
3. Multi-dimensional array
-------------------------------------------------------------
ONE DIMENSIONAL ARRAY
-------------------------------------------------------------
Example:
int arr[5] = {10,20,30,40,50};
Memory Representation:
Index: 0 1 2 3 4
Value: 10 20 30 40 50
-------------------------------------------------------------
TWO DIMENSIONAL ARRAY
-------------------------------------------------------------
Stores data in rows and columns.
Syntax:
data_type arr[row][col];
Example:
int mat[2][3] = {{1,2,3},{4,5,6}};
-------------------------------------------------------------
MULTI DIMENSIONAL ARRAY
-------------------------------------------------------------
Example:
int a[3][3][2];
-------------------------------------------------------------
ARRAY PASSING TO FUNCTION
-------------------------------------------------------------
Arrays are passed by reference.
Example:
void display(int arr[]) { ... }
-------------------------------------------------------------
INTRODUCTION TO POINTERS
-------------------------------------------------------------
A pointer stores the address of another variable.
Syntax:
data_type *ptr;
Example:
int a = 10;
int *p = &a;
-------------------------------------------------------------
ADDRESS AND DEREFERENCING
-------------------------------------------------------------
& gives address
* accesses value at address
Example:
printf("%d", *p);
-------------------------------------------------------------
POINTER ARITHMETIC
-------------------------------------------------------------
Allowed:
p++, p--, p+n, p-n
-------------------------------------------------------------
POINTER TO POINTER
-------------------------------------------------------------
int a = 10;
int *p = &a;
int **q = &p;
-------------------------------------------------------------
POINTERS AND ARRAYS
-------------------------------------------------------------
Array name behaves as pointer to first element.
Example:
int a[5] = {1,2,3,4,5};
int *p = a;
-------------------------------------------------------------
ARRAY OF POINTERS
-------------------------------------------------------------
int *p[3];
-------------------------------------------------------------
POINTER TO ARRAY
-------------------------------------------------------------
int (*p)[5];
-------------------------------------------------------------
DYNAMIC MEMORY ALLOCATION
-------------------------------------------------------------
Functions:
malloc(), calloc(), realloc(), free()
Example:
int *p = (int*)malloc(5*sizeof(int));
-------------------------------------------------------------
ARRAY & POINTER RELATIONSHIP
-------------------------------------------------------------
- Array name = constant pointer
- Pointer can traverse array
- Array size fixed, pointer flexible
-------------------------------------------------------------
PRACTICE PROGRAMS
-------------------------------------------------------------
1. Reverse array using pointer
2. Sum array using pointer
3. Matrix operations
4. Pointer-to-pointer demo
-------------------------------------------------------------
SUMMARY
-------------------------------------------------------------
Arrays store data sequentially; pointers access memory directly.
Both are essential for memory-efficient programming in C.
-------------------------------------------------------------
EXTRA EXPLANATION...
INTRODUCTION TO ARRAYS
An array is a collection of elements of the same data type stored in contiguous memory
locations.
Arrays organize similar data efficiently and support fast access using indexes.
Characteristics:
1. Fixed size
2. Same data type
3. Contiguous memory
4. Random access capability
Syntax:
data_type array_name[size];
Example:
int marks[5];
Types of Arrays:
1. One-dimensional array
2. Two-dimensional array
3. Multi-dimensional array
-------------------------------------------------------------
ONE DIMENSIONAL ARRAY
-------------------------------------------------------------
Example:
int arr[5] = {10,20,30,40,50};
Memory Representation:
Index: 0 1 2 3 4
Value: 10 20 30 40 50
-------------------------------------------------------------
TWO DIMENSIONAL ARRAY
-------------------------------------------------------------
Stores data in rows and columns.
Syntax:
data_type arr[row][col];
Example:
int mat[2][3] = {{1,2,3},{4,5,6}};
-------------------------------------------------------------
MULTI DIMENSIONAL ARRAY
-------------------------------------------------------------
Example:
int a[3][3][2];
-------------------------------------------------------------
ARRAY PASSING TO FUNCTION
-------------------------------------------------------------
Arrays are passed by reference.
Example:
void display(int arr[]) { ... }
-------------------------------------------------------------
INTRODUCTION TO POINTERS
-------------------------------------------------------------
A pointer stores the address of another variable.
Syntax:
data_type *ptr;
Example:
int a = 10;
int *p = &a;
-------------------------------------------------------------
ADDRESS AND DEREFERENCING
-------------------------------------------------------------
& gives address
* accesses value at address
Example:
printf("%d", *p);
-------------------------------------------------------------
POINTER ARITHMETIC
-------------------------------------------------------------
Allowed:
p++, p--, p+n, p-n
-------------------------------------------------------------
POINTER TO POINTER
-------------------------------------------------------------
int a = 10;
int *p = &a;
int **q = &p;
-------------------------------------------------------------
POINTERS AND ARRAYS
-------------------------------------------------------------
Array name behaves as pointer to first element.
Example:
int a[5] = {1,2,3,4,5};
int *p = a;
-------------------------------------------------------------
ARRAY OF POINTERS
-------------------------------------------------------------
int *p[3];
-------------------------------------------------------------
POINTER TO ARRAY
-------------------------------------------------------------
int (*p)[5];
-------------------------------------------------------------
DYNAMIC MEMORY ALLOCATION
-------------------------------------------------------------
Functions:
malloc(), calloc(), realloc(), free()
Example:
int *p = (int*)malloc(5*sizeof(int));
-------------------------------------------------------------
ARRAY & POINTER RELATIONSHIP
-------------------------------------------------------------
- Array name = constant pointer
- Pointer can traverse array
- Array size fixed, pointer flexible
-------------------------------------------------------------
PRACTICE PROGRAMS
-------------------------------------------------------------
1. Reverse array using pointer
2. Sum array using pointer
3. Matrix operations
4. Pointer-to-pointer demo
-------------------------------------------------------------
SUMMARY
-------------------------------------------------------------
Arrays store data sequentially; pointers access memory directly.
Both are essential for memory-efficient programming in C.
-------------------------------------------------------------
EXTRA EXPLANATION...
INTRODUCTION TO ARRAYS
An array is a collection of elements of the same data type stored in contiguous memory
locations.
Arrays organize similar data efficiently and support fast access using indexes.
Characteristics:
1. Fixed size
2. Same data type
3. Contiguous memory
4. Random access capability
Syntax:
data_type array_name[size];
Example:
int marks[5];
Types of Arrays:
1. One-dimensional array
2. Two-dimensional array
3. Multi-dimensional array
-------------------------------------------------------------
ONE DIMENSIONAL ARRAY
-------------------------------------------------------------
Example:
int arr[5] = {10,20,30,40,50};
Memory Representation:
Index: 0 1 2 3 4
Value: 10 20 30 40 50
-------------------------------------------------------------
TWO DIMENSIONAL ARRAY
-------------------------------------------------------------
Stores data in rows and columns.
Syntax:
data_type arr[row][col];
Example:
int mat[2][3] = {{1,2,3},{4,5,6}};
-------------------------------------------------------------
MULTI DIMENSIONAL ARRAY
-------------------------------------------------------------
Example:
int a[3][3][2];
-------------------------------------------------------------
ARRAY PASSING TO FUNCTION
-------------------------------------------------------------
Arrays are passed by reference.
Example:
void display(int arr[]) { ... }
-------------------------------------------------------------
INTRODUCTION TO POINTERS
-------------------------------------------------------------
A pointer stores the address of another variable.
Syntax:
data_type *ptr;
Example:
int a = 10;
int *p = &a;
-------------------------------------------------------------
ADDRESS AND DEREFERENCING
-------------------------------------------------------------
& gives address
* accesses value at address
Example:
printf("%d", *p);
-------------------------------------------------------------
POINTER ARITHMETIC
-------------------------------------------------------------
Allowed:
p++, p--, p+n, p-n
-------------------------------------------------------------
POINTER TO POINTER
-------------------------------------------------------------
int a = 10;
int *p = &a;
int **q = &p;
-------------------------------------------------------------
POINTERS AND ARRAYS
-------------------------------------------------------------
Array name behaves as pointer to first element.
Example:
int a[5] = {1,2,3,4,5};
int *p = a;
-------------------------------------------------------------
ARRAY OF POINTERS
-------------------------------------------------------------
int *p[3];
-------------------------------------------------------------
POINTER TO ARRAY
-------------------------------------------------------------
int (*p)[5];
-------------------------------------------------------------
DYNAMIC MEMORY ALLOCATION
-------------------------------------------------------------
Functions:
malloc(), calloc(), realloc(), free()
Example:
int *p = (int*)malloc(5*sizeof(int));
-------------------------------------------------------------
ARRAY & POINTER RELATIONSHIP
-------------------------------------------------------------
- Array name = constant pointer
- Pointer can traverse array
- Array size fixed, pointer flexible
-------------------------------------------------------------
PRACTICE PROGRAMS
-------------------------------------------------------------
1. Reverse array using pointer
2. Sum array using pointer
3. Matrix operations
4. Pointer-to-pointer demo
-------------------------------------------------------------
SUMMARY
-------------------------------------------------------------
Arrays store data sequentially; pointers access memory directly.
Both are essential for memory-efficient programming in C.
-------------------------------------------------------------
EXTRA EXPLANATION...
INTRODUCTION TO ARRAYS
An array is a collection of elements of the same data type stored in contiguous memory
locations.
Arrays organize similar data efficiently and support fast access using indexes.
Characteristics:
1. Fixed size
2. Same data type
3. Contiguous memory
4. Random access capability
Syntax:
data_type array_name[size];
Example:
int marks[5];
Types of Arrays:
1. One-dimensional array
2. Two-dimensional array
3. Multi-dimensional array
-------------------------------------------------------------
ONE DIMENSIONAL ARRAY
-------------------------------------------------------------
Example:
int arr[5] = {10,20,30,40,50};
Memory Representation:
Index: 0 1 2 3 4
Value: 10 20 30 40 50
-------------------------------------------------------------
TWO DIMENSIONAL ARRAY
-------------------------------------------------------------
Stores data in rows and columns.
Syntax:
data_type arr[row][col];
Example:
int mat[2][3] = {{1,2,3},{4,5,6}};
-------------------------------------------------------------
MULTI DIMENSIONAL ARRAY
-------------------------------------------------------------
Example:
int a[3][3][2];
-------------------------------------------------------------
ARRAY PASSING TO FUNCTION
-------------------------------------------------------------
Arrays are passed by reference.
Example:
void display(int arr[]) { ... }
-------------------------------------------------------------
INTRODUCTION TO POINTERS
-------------------------------------------------------------
A pointer stores the address of another variable.
Syntax:
data_type *ptr;
Example:
int a = 10;
int *p = &a;
-------------------------------------------------------------
ADDRESS AND DEREFERENCING
-------------------------------------------------------------
& gives address
* accesses value at address
Example:
printf("%d", *p);
-------------------------------------------------------------
POINTER ARITHMETIC
-------------------------------------------------------------
Allowed:
p++, p--, p+n, p-n
-------------------------------------------------------------
POINTER TO POINTER
-------------------------------------------------------------
int a = 10;
int *p = &a;
int **q = &p;
-------------------------------------------------------------
POINTERS AND ARRAYS
-------------------------------------------------------------
Array name behaves as pointer to first element.
Example:
int a[5] = {1,2,3,4,5};
int *p = a;
-------------------------------------------------------------
ARRAY OF POINTERS
-------------------------------------------------------------
int *p[3];
-------------------------------------------------------------
POINTER TO ARRAY
-------------------------------------------------------------
int (*p)[5];
-------------------------------------------------------------
DYNAMIC MEMORY ALLOCATION
-------------------------------------------------------------
Functions:
malloc(), calloc(), realloc(), free()
Example:
int *p = (int*)malloc(5*sizeof(int));
-------------------------------------------------------------
ARRAY & POINTER RELATIONSHIP
-------------------------------------------------------------
- Array name = constant pointer
- Pointer can traverse array
- Array size fixed, pointer flexible
-------------------------------------------------------------
PRACTICE PROGRAMS
-------------------------------------------------------------
1. Reverse array using pointer
2. Sum array using pointer
3. Matrix operations
4. Pointer-to-pointer demo
-------------------------------------------------------------
SUMMARY
-------------------------------------------------------------
Arrays store data sequentially; pointers access memory directly.
Both are essential for memory-efficient programming in C.
-------------------------------------------------------------
EXTRA EXPLANATION...
Characteristics:
1. Fixed size
2. Same data type
3. Contiguous memory
4. Random access capability
Syntax:
data_type array_name[size];
Example:
int marks[5];
Types of Arrays:
1. One-dimensional array
2. Two-dimensional array
3. Multi-dimensional array
-------------------------------------------------------------
ONE DIMENSIONAL ARRAY
-------------------------------------------------------------
Example:
int arr[5] = {10,20,30,40,50};
Memory Representation:
Index: 0 1 2 3 4
Value: 10 20 30 40 50
-------------------------------------------------------------
TWO DIMENSIONAL ARRAY
-------------------------------------------------------------
Stores data in rows and columns.
Syntax:
data_type arr[row][col];
Example:
int mat[2][3] = {{1,2,3},{4,5,6}};
-------------------------------------------------------------
MULTI DIMENSIONAL ARRAY
-------------------------------------------------------------
Example:
int a[3][3][2];
-------------------------------------------------------------
ARRAY PASSING TO FUNCTION
-------------------------------------------------------------
Arrays are passed by reference.
Example:
void display(int arr[]) { ... }
-------------------------------------------------------------
INTRODUCTION TO POINTERS
-------------------------------------------------------------
A pointer stores the address of another variable.
Syntax:
data_type *ptr;
Example:
int a = 10;
int *p = &a;
-------------------------------------------------------------
ADDRESS AND DEREFERENCING
-------------------------------------------------------------
& gives address
* accesses value at address
Example:
printf("%d", *p);
-------------------------------------------------------------
POINTER ARITHMETIC
-------------------------------------------------------------
Allowed:
p++, p--, p+n, p-n
-------------------------------------------------------------
POINTER TO POINTER
-------------------------------------------------------------
int a = 10;
int *p = &a;
int **q = &p;
-------------------------------------------------------------
POINTERS AND ARRAYS
-------------------------------------------------------------
Array name behaves as pointer to first element.
Example:
int a[5] = {1,2,3,4,5};
int *p = a;
-------------------------------------------------------------
ARRAY OF POINTERS
-------------------------------------------------------------
int *p[3];
-------------------------------------------------------------
POINTER TO ARRAY
-------------------------------------------------------------
int (*p)[5];
-------------------------------------------------------------
DYNAMIC MEMORY ALLOCATION
-------------------------------------------------------------
Functions:
malloc(), calloc(), realloc(), free()
Example:
int *p = (int*)malloc(5*sizeof(int));
-------------------------------------------------------------
ARRAY & POINTER RELATIONSHIP
-------------------------------------------------------------
- Array name = constant pointer
- Pointer can traverse array
- Array size fixed, pointer flexible
-------------------------------------------------------------
PRACTICE PROGRAMS
-------------------------------------------------------------
1. Reverse array using pointer
2. Sum array using pointer
3. Matrix operations
4. Pointer-to-pointer demo
-------------------------------------------------------------
SUMMARY
-------------------------------------------------------------
Arrays store data sequentially; pointers access memory directly.
Both are essential for memory-efficient programming in C.
-------------------------------------------------------------
EXTRA EXPLANATION...
INTRODUCTION TO ARRAYS
An array is a collection of elements of the same data type stored in contiguous memory
locations.
Arrays organize similar data efficiently and support fast access using indexes.
Characteristics:
1. Fixed size
2. Same data type
3. Contiguous memory
4. Random access capability
Syntax:
data_type array_name[size];
Example:
int marks[5];
Types of Arrays:
1. One-dimensional array
2. Two-dimensional array
3. Multi-dimensional array
-------------------------------------------------------------
ONE DIMENSIONAL ARRAY
-------------------------------------------------------------
Example:
int arr[5] = {10,20,30,40,50};
Memory Representation:
Index: 0 1 2 3 4
Value: 10 20 30 40 50
-------------------------------------------------------------
TWO DIMENSIONAL ARRAY
-------------------------------------------------------------
Stores data in rows and columns.
Syntax:
data_type arr[row][col];
Example:
int mat[2][3] = {{1,2,3},{4,5,6}};
-------------------------------------------------------------
MULTI DIMENSIONAL ARRAY
-------------------------------------------------------------
Example:
int a[3][3][2];
-------------------------------------------------------------
ARRAY PASSING TO FUNCTION
-------------------------------------------------------------
Arrays are passed by reference.
Example:
void display(int arr[]) { ... }
-------------------------------------------------------------
INTRODUCTION TO POINTERS
-------------------------------------------------------------
A pointer stores the address of another variable.
Syntax:
data_type *ptr;
Example:
int a = 10;
int *p = &a;
-------------------------------------------------------------
ADDRESS AND DEREFERENCING
-------------------------------------------------------------
& gives address
* accesses value at address
Example:
printf("%d", *p);
-------------------------------------------------------------
POINTER ARITHMETIC
-------------------------------------------------------------
Allowed:
p++, p--, p+n, p-n
-------------------------------------------------------------
POINTER TO POINTER
-------------------------------------------------------------
int a = 10;
int *p = &a;
int **q = &p;
-------------------------------------------------------------
POINTERS AND ARRAYS
-------------------------------------------------------------
Array name behaves as pointer to first element.
Example:
int a[5] = {1,2,3,4,5};
int *p = a;
-------------------------------------------------------------
ARRAY OF POINTERS
-------------------------------------------------------------
int *p[3];
-------------------------------------------------------------
POINTER TO ARRAY
-------------------------------------------------------------
int (*p)[5];
-------------------------------------------------------------
DYNAMIC MEMORY ALLOCATION
-------------------------------------------------------------
Functions:
malloc(), calloc(), realloc(), free()
Example:
int *p = (int*)malloc(5*sizeof(int));
-------------------------------------------------------------
ARRAY & POINTER RELATIONSHIP
-------------------------------------------------------------
- Array name = constant pointer
- Pointer can traverse array
- Array size fixed, pointer flexible
-------------------------------------------------------------
PRACTICE PROGRAMS
-------------------------------------------------------------
1. Reverse array using pointer
2. Sum array using pointer
3. Matrix operations
4. Pointer-to-pointer demo
-------------------------------------------------------------
SUMMARY
-------------------------------------------------------------
Arrays store data sequentially; pointers access memory directly.
Both are essential for memory-efficient programming in C.
-------------------------------------------------------------
EXTRA EXPLANATION...
INTRODUCTION TO ARRAYS
An array is a collection of elements of the same data type stored in contiguous memory
locations.
Arrays organize similar data efficiently and support fast access using indexes.
Characteristics:
1. Fixed size
2. Same data type
3. Contiguous memory
4. Random access capability
Syntax:
data_type array_name[size];
Example:
int marks[5];
Types of Arrays:
1. One-dimensional array
2. Two-dimensional array
3. Multi-dimensional array
-------------------------------------------------------------
ONE DIMENSIONAL ARRAY
-------------------------------------------------------------
Example:
int arr[5] = {10,20,30,40,50};
Memory Representation:
Index: 0 1 2 3 4
Value: 10 20 30 40 50
-------------------------------------------------------------
TWO DIMENSIONAL ARRAY
-------------------------------------------------------------
Stores data in rows and columns.
Syntax:
data_type arr[row][col];
Example:
int mat[2][3] = {{1,2,3},{4,5,6}};
-------------------------------------------------------------
MULTI DIMENSIONAL ARRAY
-------------------------------------------------------------
Example:
int a[3][3][2];
-------------------------------------------------------------
ARRAY PASSING TO FUNCTION
-------------------------------------------------------------
Arrays are passed by reference.
Example:
void display(int arr[]) { ... }
-------------------------------------------------------------
INTRODUCTION TO POINTERS
-------------------------------------------------------------
A pointer stores the address of another variable.
Syntax:
data_type *ptr;
Example:
int a = 10;
int *p = &a;
-------------------------------------------------------------
ADDRESS AND DEREFERENCING
-------------------------------------------------------------
& gives address
* accesses value at address
Example:
printf("%d", *p);
-------------------------------------------------------------
POINTER ARITHMETIC
-------------------------------------------------------------
Allowed:
p++, p--, p+n, p-n
-------------------------------------------------------------
POINTER TO POINTER
-------------------------------------------------------------
int a = 10;
int *p = &a;
int **q = &p;
-------------------------------------------------------------
POINTERS AND ARRAYS
-------------------------------------------------------------
Array name behaves as pointer to first element.
Example:
int a[5] = {1,2,3,4,5};
int *p = a;
-------------------------------------------------------------
ARRAY OF POINTERS
-------------------------------------------------------------
int *p[3];
-------------------------------------------------------------
POINTER TO ARRAY
-------------------------------------------------------------
int (*p)[5];
-------------------------------------------------------------
DYNAMIC MEMORY ALLOCATION
-------------------------------------------------------------
Functions:
malloc(), calloc(), realloc(), free()
Example:
int *p = (int*)malloc(5*sizeof(int));
-------------------------------------------------------------
ARRAY & POINTER RELATIONSHIP
-------------------------------------------------------------
- Array name = constant pointer
- Pointer can traverse array
- Array size fixed, pointer flexible
-------------------------------------------------------------
PRACTICE PROGRAMS
-------------------------------------------------------------
1. Reverse array using pointer
2. Sum array using pointer
3. Matrix operations
4. Pointer-to-pointer demo
-------------------------------------------------------------
SUMMARY
-------------------------------------------------------------
Arrays store data sequentially; pointers access memory directly.
Both are essential for memory-efficient programming in C.
-------------------------------------------------------------
EXTRA EXPLANATION...
INTRODUCTION TO ARRAYS
An array is a collection of elements of the same data type stored in contiguous memory
locations.
Arrays organize similar data efficiently and support fast access using indexes.
Characteristics:
1. Fixed size
2. Same data type
3. Contiguous memory
4. Random access capability
Syntax:
data_type array_name[size];
Example:
int marks[5];
Types of Arrays:
1. One-dimensional array
2. Two-dimensional array
3. Multi-dimensional array
-------------------------------------------------------------
ONE DIMENSIONAL ARRAY
-------------------------------------------------------------
Example:
int arr[5] = {10,20,30,40,50};
Memory Representation:
Index: 0 1 2 3 4
Value: 10 20 30 40 50
-------------------------------------------------------------
TWO DIMENSIONAL ARRAY
-------------------------------------------------------------
Stores data in rows and columns.
Syntax:
data_type arr[row][col];
Example:
int mat[2][3] = {{1,2,3},{4,5,6}};
-------------------------------------------------------------
MULTI DIMENSIONAL ARRAY
-------------------------------------------------------------
Example:
int a[3][3][2];
-------------------------------------------------------------
ARRAY PASSING TO FUNCTION
-------------------------------------------------------------
Arrays are passed by reference.
Example:
void display(int arr[]) { ... }
-------------------------------------------------------------
INTRODUCTION TO POINTERS
-------------------------------------------------------------
A pointer stores the address of another variable.
Syntax:
data_type *ptr;
Example:
int a = 10;
int *p = &a;
-------------------------------------------------------------
ADDRESS AND DEREFERENCING
-------------------------------------------------------------
& gives address
* accesses value at address
Example:
printf("%d", *p);
-------------------------------------------------------------
POINTER ARITHMETIC
-------------------------------------------------------------
Allowed:
p++, p--, p+n, p-n
-------------------------------------------------------------
POINTER TO POINTER
-------------------------------------------------------------
int a = 10;
int *p = &a;
int **q = &p;
-------------------------------------------------------------
POINTERS AND ARRAYS
-------------------------------------------------------------
Array name behaves as pointer to first element.
Example:
int a[5] = {1,2,3,4,5};
int *p = a;
-------------------------------------------------------------
ARRAY OF POINTERS
-------------------------------------------------------------
int *p[3];
-------------------------------------------------------------
POINTER TO ARRAY
-------------------------------------------------------------
int (*p)[5];
-------------------------------------------------------------
DYNAMIC MEMORY ALLOCATION
-------------------------------------------------------------
Functions:
malloc(), calloc(), realloc(), free()
Example:
int *p = (int*)malloc(5*sizeof(int));
-------------------------------------------------------------
ARRAY & POINTER RELATIONSHIP
-------------------------------------------------------------
- Array name = constant pointer
- Pointer can traverse array
- Array size fixed, pointer flexible
-------------------------------------------------------------
PRACTICE PROGRAMS
-------------------------------------------------------------
1. Reverse array using pointer
2. Sum array using pointer
3. Matrix operations
4. Pointer-to-pointer demo
-------------------------------------------------------------
SUMMARY
-------------------------------------------------------------
Arrays store data sequentially; pointers access memory directly.
Both are essential for memory-efficient programming in C.
-------------------------------------------------------------
EXTRA EXPLANATION...