0% found this document useful (0 votes)
9 views

pointer_exercises

This document provides a series of C programming exercises focused on pointers, double pointers, arrays, and arrays of pointers. Each exercise includes a problem statement followed by a code example that demonstrates the solution. The exercises aim to enhance understanding of fundamental pointer concepts in C programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

pointer_exercises

This document provides a series of C programming exercises focused on pointers, double pointers, arrays, and arrays of pointers. Each exercise includes a problem statement followed by a code example that demonstrates the solution. The exercises aim to enhance understanding of fundamental pointer concepts in C programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

C Programming Exercises: Pointers, Arrays,

and Double Pointers


This document contains exercises on pointers, double pointers, arrays, and arrays of
pointers in C.

1. Basic Pointer Usage


Problem: Declare an integer and a pointer to it. Print the value and address.

#include <stdio.h>

int main() {
int x = 10;
int *ptr = &x;

printf("Value of x: %d\n", *ptr);


printf("Address of x: %p\n", ptr);

return 0;
}

2. Modify Value Using Pointer


Problem: Write a function that modifies a variable using a pointer.

#include <stdio.h>

void modifyValue(int *ptr) {


*ptr = 50;
}

int main() {
int num = 10;
printf("Before: %d\n", num);
modifyValue(&num);
printf("After: %d\n", num);

return 0;
}

3. Swap Two Numbers Using Pointers


Problem: Swap two integers using pointers.

#include <stdio.h>

void swap(int *a, int *b) {


int temp = *a;
*a = *b;
*b = temp;
}

int main() {
int x = 5, y = 10;
printf("Before swap: x=%d, y=%d\n", x, y);
swap(&x, &y);
printf("After swap: x=%d, y=%d\n", x, y);

return 0;
}

4. Double Pointer Demonstration


Problem: Modify a value using a double pointer.

#include <stdio.h>

void modifyUsingDoublePointer(int **pp) {


**pp = 30;
}

int main() {
int x = 10;
int *ptr = &x;
int **pptr = &ptr;

printf("Before: %d\n", x);


modifyUsingDoublePointer(pptr);
printf("After: %d\n", x);

return 0;
}

5. Pointer Arithmetic on Arrays


Problem: Print an array using pointer arithmetic.

#include <stdio.h>

int main() {
int arr[] = {1, 2, 3, 4, 5};
int *ptr = arr;

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


printf("%d ", *(ptr + i));
}

return 0;
}
6. Find Largest Element in an Array Using Pointers
Problem: Find the maximum element in an array.

#include <stdio.h>

int findMax(int *arr, int size) {


int max = *arr;
for (int i = 1; i < size; i++) {
if (*(arr + i) > max) {
max = *(arr + i);
}
}
return max;
}

int main() {
int numbers[] = {3, 7, 2, 8, 5};
printf("Max element: %d\n", findMax(numbers, 5));

return 0;
}

7. Array of Pointers
Problem: Store values using an array of pointers.

#include <stdio.h>

int main() {
int a = 10, b = 20, c = 30;
int *arr[] = {&a, &b, &c};

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


printf("%d ", *arr[i]);
}

return 0;
}

8. Array of String Pointers


Problem: Store and print a list of names using an array of character pointers.

#include <stdio.h>

int main() {
char *names[] = {"Alice", "Bob", "Charlie"};

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


printf("%s\n", names[i]);
}

return 0;
}

9. Sorting Strings Using Array of Pointers


Problem: Sort an array of strings.

#include <stdio.h>
#include <string.h>

void sortStrings(char *arr[], int n) {


for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (strcmp(arr[i], arr[j]) > 0) {
char *temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}

int main() {
char *words[] = {"banana", "apple", "cherry"};
int n = 3;

sortStrings(words, n);

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


printf("%s\n", words[i]);
}

return 0;
}

10. Array of Pointers Pointing to Elements of Another


Array
Problem: Create an array of pointers, where each pointer points to a different element
of another array.

#include <stdio.h>

int main() {
int numbers[] = {10, 20, 30, 40, 50};
int *ptrArray[5];
for (int i = 0; i < 5; i++) {
ptrArray[i] = &numbers[i];
}

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


printf("%d ", *ptrArray[i]);
}

return 0;
}

11. Array of Pointers Pointing to Selected Elements of


Another Array
Problem: Create an array of 10 integers, and an array of 5 pointers that point to
selected elements from the first array.

#include <stdio.h>

int main() {
int numbers[10] = {5, 10, 15, 20, 25, 30, 35, 40, 45, 50};
int *ptrArray[5];

ptrArray[0] = &numbers[0];
ptrArray[1] = &numbers[2];
ptrArray[2] = &numbers[4];
ptrArray[3] = &numbers[6];
ptrArray[4] = &numbers[8];

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


printf("%d ", *ptrArray[i]);
}

return 0;
}

Conclusion
These exercises cover fundamental pointer concepts in C, including:

Pointer basics
Pointer arithmetic
Double pointers
Arrays and pointers
Sorting with pointers

Practice these problems to strengthen your understanding of pointers in C!

You might also like