0% found this document useful (0 votes)
10 views51 pages

Arrays and Pointers in C Explained

Uploaded by

khushbu
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views51 pages

Arrays and Pointers in C Explained

Uploaded by

khushbu
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Arrays and Pointers – Detailed Notes

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};

Elements accessed using indexes:


arr[0], arr[1], etc.

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}};

C uses Row-major order storage.

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

Moves by size of datatype.

-------------------------------------------------------------
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};

Elements accessed using indexes:


arr[0], arr[1], etc.

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}};

C uses Row-major order storage.

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

Moves by size of datatype.

-------------------------------------------------------------
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};

Elements accessed using indexes:


arr[0], arr[1], etc.

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}};

C uses Row-major order storage.

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

Moves by size of datatype.

-------------------------------------------------------------
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};

Elements accessed using indexes:


arr[0], arr[1], etc.

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}};

C uses Row-major order storage.


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

Moves by size of datatype.


-------------------------------------------------------------
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};

Elements accessed using indexes:


arr[0], arr[1], etc.

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}};

C uses Row-major order storage.

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

Moves by size of datatype.

-------------------------------------------------------------
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};

Elements accessed using indexes:


arr[0], arr[1], etc.

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}};

C uses Row-major order storage.

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

Moves by size of datatype.

-------------------------------------------------------------
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};

Elements accessed using indexes:


arr[0], arr[1], etc.

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}};

C uses Row-major order storage.

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

Moves by size of datatype.

-------------------------------------------------------------
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};

Elements accessed using indexes:


arr[0], arr[1], etc.

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}};

C uses Row-major order storage.

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

Moves by size of datatype.

-------------------------------------------------------------
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};

Elements accessed using indexes:


arr[0], arr[1], etc.

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}};

C uses Row-major order storage.

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

Moves by size of datatype.

-------------------------------------------------------------
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};

Elements accessed using indexes:


arr[0], arr[1], etc.

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}};

C uses Row-major order storage.

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

Moves by size of datatype.

-------------------------------------------------------------
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};

Elements accessed using indexes:


arr[0], arr[1], etc.

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}};

C uses Row-major order storage.

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

Moves by size of datatype.

-------------------------------------------------------------
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};

Elements accessed using indexes:


arr[0], arr[1], etc.

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}};

C uses Row-major order storage.

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

Moves by size of datatype.

-------------------------------------------------------------
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};

Elements accessed using indexes:


arr[0], arr[1], etc.

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}};

C uses Row-major order storage.

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

Moves by size of datatype.

-------------------------------------------------------------
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};

Elements accessed using indexes:


arr[0], arr[1], etc.

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}};

C uses Row-major order storage.

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

Moves by size of datatype.

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

You might also like