Day_5 C-Programming
Day_5 C-Programming
Pointers
Definition
A pointer is a variable that stores the memory address of another variable. Pointers
allow for efficient memory management and enable dynamic memory allocation,
passing data by reference, and creating complex data structures like linked lists and
trees.
· Every variable is a memory location and every memory location has its address
defined which can be accessed using ampersand (&) operator, which denotes an
address in memory.
Syntax:
<datatype> *pointerName;
Example:
#include <stdio.h>
int main() {
int x = 10;
return 0;
Output:
Value of x: 10
Address of x: 0x7ffee1a4c98c
Valid Operations:
1. Increment (ptr++) and decrement (ptr--) pointers.
2. Add/subtract integers to/from pointers (ptr + n, ptr - n).
3. Subtract one pointer from another to calculate the number of elements between
them.
Example:
#include <stdio.h>
int main() {
printf("Pointer arithmetic:\n");
ptr++;
ptr += 2;
return 0;
Output:
Pointer arithmetic:
Value at ptr: 10
Value at ptr++: 20
Value at ptr+2: 40
Scaling
Pointer Aliasing
Pointer aliasing occurs when two or more pointers point to the same memory location.
Modifying the data through one pointer affects the data accessed through the other
pointer(s).
Example:
#include <stdio.h>
int main() {
int x = 100;
return 0;
Output:
Call by Reference
In the call by reference method, the function receives the address of the actual
parameter. Changes made to the parameter inside the function affect the original value.
Example:
#include <stdio.h>
*a = *b;
*b = temp;
int main() {
swap(&x, &y);
return 0;
Output:
Syntax:
<datatype> arrayName[size];
Example:
#include <stdio.h>
int main() {
int arr[5] = {10, 20, 30, 40, 50}; // Declaration and initialization
return 0;
}
Output:
arr[0] = 10
arr[1] = 20
arr[2] = 30
arr[3] = 40
arr[4] = 50
Array elements can be accessed using pointers because the array name
represents the address of the first element.
Example:
#include <stdio.h>
int main() {
return 0;
Output:
Element 0: 10
Element 1: 20
Element 2: 30
Element 3: 40
Element 4: 50
Example:
#include <stdio.h>
int main() {
printf("Original array:\n");
// Modify elements
arr[i] *= 2;
printf("Modified array:\n");
return 0;
Output:
Original array:
arr[0] = 1
arr[1] = 2
arr[2] = 3
arr[3] = 4
arr[4] = 5
Modified array:
arr[0] = 2
arr[1] = 4
arr[2] = 6
arr[3] = 8
arr[4] = 10
Linear Search
Linear search involves searching for an element by checking each array element
sequentially.
Algorithm:
#include <stdio.h>
if (arr[i] == key) {
int main() {
if (result != -1) {
} else {
printf("Element %d not found\n", key);
return 0;
Output:
Binary Search
Algorithm:
Example:
#include <stdio.h>
{
int low = 0, high = size - 1;
if (arr[mid] == key) {
return mid;
low = mid + 1;
} else {
high = mid - 1;
int main() {
} else {
return 0;
Output:
Bubble Sort
Algorithm:
Example:
#include <stdio.h>
// Swap
arr[j + 1] = temp;
int main() {
printf("Original array:\n");
}
printf("\n");
bubbleSort(arr, 5);
printf("Sorted array:\n");
printf("\n");
return 0;
Output:
Original array:
50 20 40 10 30
Sorted array:
10 20 30 40 50
Practice Problems
1. Write a program to find the maximum and minimum elements in an array
using pointers.
2. Implement linear search to count the occurrences of a target value in an
array.
3. Write a program to reverse an array using pointers.
4. Implement binary search on a user-input sorted array.
5. Modify the bubble sort algorithm to sort in descending order.
6. Write a program to find the sum and average of array elements using
pointers.
7. Implement a program to merge two sorted arrays into one sorted array.
8. Create a program to find the second largest element in an array.
9. Implement bubble sort using a while loop instead of nested for loops.
10. Write a program to count the number of even and odd elements in an
array.
11.Write a program to find the largest of three numbers using call by
reference.
12. Implement pointer arithmetic to traverse an array and find the sum of its
elements.
13. Demonstrate pointer aliasing by swapping two numbers without using a
temporary variable.
14. Write a program to reverse a string using pointers.
15. Implement a function to find the factorial of a number using call by
reference.
16. Demonstrate the difference between pointer arithmetic on int and char
types.
17. Write a program to dynamically allocate memory for an array and find
its average using pointers.
18. Implement matrix multiplication using pointers.
19. Write a program to compare two strings using pointers.
20. Implement a program to find the length of a string using pointer
arithmetic.