1. Program: Pointer Basics
#include <stdio.h>
int main() {
int num = 25;
int *ptr;
ptr = #
printf("Value of num using pointer: %d\n", *ptr);
printf("Address of num using pointer: %p\n", ptr);
printf("Address of num directly: %p\n", &num);
return 0;
}
Explanation:
int *ptr
: Declares a pointer ptr
that can hold the address of an integer variable.
ptr = &num
: Assigns the address of the variable num
to the pointer ptr
.
*ptr
: Dereferences the pointer to access the value stored at the memory location ptr
is pointing to.
ptr
: Directly prints the memory address stored in the pointer.
Sample Output:
Value of num using pointer: 25
Address of num using pointer: 0x7ffee4f2b98c
Address of num directly: 0x7ffee4f2b98c
2. Program: Sum of Two Numbers Using Pointers
#include <stdio.h>
int main() {
int num1, num2, sum;
int *ptr1, *ptr2;
// Input two numbers
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
// Assign the addresses of num1 and num2 to the pointers
ptr1 = &num1;
ptr2 = &num2;
// Calculate the sum using pointers
sum = *ptr1 + *ptr2;
// Print the result
printf("The sum of %d and %d is: %d\n", *ptr1, *ptr2, sum);
return 0;
}
Explanation:
int *ptr1, *ptr2
: Declares two pointers ptr1
and ptr2
to store the addresses of the integers num1
and num2
.
ptr1 = &num1
and ptr2 = &num2
: Assign the addresses of num1
and num2
to the pointers.
sum = *ptr1 + *ptr2
: Dereferences the pointers to get the values of num1
and num2
, adds them together, and stores the result in sum
.
Sample Output:
Enter the first number: 10
Enter the second number: 20
The sum of 10 and 20 is: 30
3. Program: Largest of Three Numbers Using Pointers
#include <stdio.h>
int main() {
int num1, num2, num3;
int *ptr1, *ptr2, *ptr3;
int *largest;
// Input three numbers
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
printf("Enter the third number: ");
scanf("%d", &num3);
// Assign the addresses of num1, num2, and num3 to the pointers
ptr1 = &num1;
ptr2 = &num2;
ptr3 = &num3;
// Find the largest number using pointers
largest = ptr1;
if (*ptr2 > *largest) {
largest = ptr2; // Update largest if num2 is greater
}
if (*ptr3 > *largest) {
largest = ptr3; // Update largest if num3 is greater
}
// Print the largest number
printf("The largest of %d, %d, and %d is: %d\n", *ptr1, *ptr2, *ptr3, *largest);
return 0;
}
Explanation:
int *ptr1, *ptr2, *ptr3
: Declares three pointers to store the addresses of the three integers num1
, num2
, and num3
.
largest = ptr1
: Assumes the first number is the largest initially.
- The program then compares the second (
num2
) and third (num3
) numbers with the current largest and updates the largest
pointer accordingly.
*largest
: Dereferences the pointer to access and print the largest number.
Sample Output:
Enter the first number: 10
Enter the second number: 25
Enter the third number: 15
The largest of 10, 25, and 15 is: 25
4. Program: Pointer Arithmetic in C
#include <stdio.h>
int main() {
int arr[5] = {10, 20, 30, 40, 50};
int *ptr = arr;
// Accessing elements using pointer
printf("Initial pointer points to the first element: %d\n", *ptr);
ptr++;
printf("After incrementing, pointer points to the second element: %d\n", *ptr);
ptr++;
printf("After incrementing again, pointer points to the third element: %d\n", *ptr);
ptr--;
printf("After decrementing, pointer points back to the second element: %d\n", *ptr);
ptr = arr; // Reset pointer to the first element
printf("Accessing the last element using pointer arithmetic: %d\n", *(ptr + 4));
// Printing all elements of the array using the pointer and pointer arithmetic
printf("All elements of the array:\n");
for (int i = 0; i < 5; i++) {
printf("Element %d: %d\n", i, *(ptr + i));
}
return 0;
}
Explanation:
ptr = arr
: The pointer ptr
is initialized to point to the first element of the array arr
.
ptr++
: Incrementing the pointer moves it to the next element in the array (increments by the size of the data type, here int
).
ptr--
: Decrementing the pointer moves it back to the previous element.
*(ptr + 4)
: Accesses the fifth element of the array using pointer arithmetic.
- The program also demonstrates how to iterate over the array elements using pointer arithmetic inside a loop.
Sample Output:
Initial pointer points to the first element: 10
After incrementing, pointer points to the second element: 20
After incrementing again, pointer points to the third element: 30
After decrementing, pointer points back to the second element: 20
Accessing the last element using pointer arithmetic: 50
All elements of the array:
Element 0: 10
Element 1: 20
Element 2: 30
Element 3: 40
Element 4: 50