You are on page 1of 9

1.

Write a c program to display the address of a float and int type variable using pointers

#include <stdio.h>

int main() {
// Declare variables
int integerValue;
float floatValue;

// Assign values to variables


integerValue = 10;
floatValue = 3.14;

// Declare pointer variables


int *intPointer;
float *floatPointer;

// Assign addresses to pointers


intPointer = &integerValue;
floatPointer = &floatValue;

// Display addresses
printf("Address of integer variable: %p\n", intPointer);
printf("Address of float variable: %p\n", floatPointer);

return 0;
}
2. WAP in C to print the contents of an integer array by using Pointers.

#include <stdio.h>

int main() {

// Declare an integer variable

int integerValue = 42;

// Declare a pointer to the integer

int *ptr = &integerValue;

// Display the original memory address

printf("Original Memory Address: %p\n", (void *)ptr);

// Increment the pointer

ptr++;

// Display the new memory address after incrementing

printf("New Memory Address (After Incrementing): %p\n", (void *)ptr);

// Decrement the pointer

ptr--;

// Display the new memory address after decrementing

printf("New Memory Address (After Decrementing): %p\n", (void *)ptr);

return 0;

}
3. Write a c program to show the change of memory address that is observed after incrementing the pointer to an
integer

#include <stdio.h>

int main() {

// Declare an integer variable

int integerValue = 42;

// Declare a pointer to the integer

int *ptr = &integerValue;

// Display the original memory address

printf("Original Memory Address: %p\n", (void *)ptr);

// Increment the pointer

ptr++;

// Display the new memory address

printf("New Memory Address: %p\n", (void *)ptr);

return 0;

}
4. Write a c program to show the change of memory address that is observed after incrementing & decrementing
the pointer to a float variable.

#include <stdio.h>

int main() {

// Declare a float variable

float floatValue = 3.14;

// Declare a pointer to the float

float *ptr = &floatValue;

// Display the original memory address

printf("Original Memory Address: %p\n", (void *)ptr);

// Increment the pointer

ptr++;

// Display the new memory address after incrementing

printf("New Memory Address (After Incrementing): %p\n", (void *)ptr);

// Decrement the pointer

ptr--;

// Display the new memory address after decrementing

printf("New Memory Address (After Decrementing): %p\n", (void *)ptr);

return 0;

}
5. Write a program to add and subtract 2 integers using pointers

#include <stdio.h>

int main() {

// Declare variables

int num1, num2, sum, difference;

// Get input from the user

printf("Enter the first integer: ");

scanf("%d", &num1);

printf("Enter the second integer: ");

scanf("%d", &num2);

// Declare pointers

int *ptr1 = &num1;

int *ptr2 = &num2;

// Perform addition using pointers

sum = *ptr1 + *ptr2;

// Perform subtraction using pointers

difference = *ptr1 - *ptr2;

// Display results

printf("Sum: %d + %d = %d\n", num1, num2, sum);

printf("Difference: %d - %d = %d\n", num1, num2, difference);

return 0;

}
6. Write a program to find the largest and smallest elements of an array using pointers

#include <stdio.h>

int main() {

// Declare variables

int size;

// Get the size of the array from the user

printf("Enter the size of the array: ");

scanf("%d", &size);

// Check if the size is non-positive

if (size <= 0) {

printf("Invalid array size\n");

return 1; // Exit with an error code

// Declare the array

int arr[size];

// Get input for the array elements

printf("Enter the elements of the array:\n");

for (int i = 0; i < size; i++) {

printf("Element %d: ", i + 1);

scanf("%d", &arr[i]);

// Declare pointers for the largest and smallest elements

int *ptrLargest = &arr[0];

int *ptrSmallest = &arr[0];

// Find the largest and smallest elements using pointers

for (int i = 1; i < size; i++) {


if (*ptrLargest < arr[i]) {

ptrLargest = &arr[i];

if (*ptrSmallest > arr[i]) {

ptrSmallest = &arr[i];

// Display the largest and smallest elements

printf("Largest Element: %d\n", *ptrLargest);

printf("Smallest Element: %d\n", *ptrSmallest);

return 0;

7. Write a program to swap the values of 2 integers with and without using pointers

Without using pointers:

#include <stdio.h>

int main() {
// Declare variables
int num1, num2;

// Get input from the user


printf("Enter the first integer: ");
scanf("%d", &num1);

printf("Enter the second integer: ");


scanf("%d", &num2);

// Display original values


printf("Original Values: num1 = %d, num2 = %d\n", num1, num2);

// Swap values without using pointers


int temp = num1;
num1 = num2;
num2 = temp;

// Display swapped values


printf("Swapped Values (Without Pointers): num1 = %d, num2 = %d\n", num1, num2);

return 0;
}
Using pointers:

#include <stdio.h>

int main() {
// Declare variables
int num1, num2;

// Get input from the user


printf("Enter the first integer: ");
scanf("%d", &num1);

printf("Enter the second integer: ");


scanf("%d", &num2);

// Display original values


printf("Original Values: num1 = %d, num2 = %d\n", num1, num2);

// Swap values using pointers


int *ptr1 = &num1;
int *ptr2 = &num2;

// Swap values using pointers


int temp = *ptr1;
*ptr1 = *ptr2;
*ptr2 = temp;

// Display swapped values


printf("Swapped Values (With Pointers): num1 = %d, num2 = %d\n", num1, num2);

return 0;
}

8. Write a c program to demonstrate pointer to pointer

#include <stdio.h>

int main() {
// Declare variables
int value = 42;
int *ptr1 = &value; // Pointer to an integer
int **ptr2 = &ptr1; // Pointer to a pointer to an integer

// Display the original value and its address


printf("Original Value: %d\n", value);
printf("Address of Value: %p\n", (void *)ptr1);

// Display the value through a pointer to a pointer


printf("Value through Pointer to Pointer: %d\n", **ptr2);

// Modify the value through a pointer to a pointer


**ptr2 = 99;

// Display the modified value and its address


printf("Modified Value: %d\n", value);
printf("Address of Value: %p\n", (void *)ptr1);

return 0;
}

You might also like