C (programming language)

Pointers in C Exercises and Solutions

Mastering pointers is crucial for effective C programming. These exercises will challenge your knowledge and solidify your understanding of pointers concepts.

Understanding Pointer Basics

1. Declaring and Initializing Pointers

Task: Declare a pointer variable ptr that can point to an integer, and initialize it to point to the address of an integer variable num.

Solution:

C

int num = 10;
int *ptr = #

2. Accessing Values Using Pointers

Task: Print the value of the integer variable num using both direct access and pointer dereferencing.

Solution:

C

printf("Value of num (direct access): %d\n", num);
printf("Value of num (using pointer): %d\n", *ptr);

3. Pointer Arithmetic

Task: Create an array arr of 5 integers, and use a pointer to access and print each element of the array.

Solution:

C

int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr;  // Pointer points to the first element of the array

for (int i = 0; i < 5; i++) {
    printf("%d ", *ptr);
    ptr++;  // Increment the pointer to point to the next element
}

Pointers and Functions

4. Passing Pointers to Functions

Task: Write a function swap that takes two integer pointers as arguments and swaps the values they point to.

Solution:

C

void swap(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}

5. Returning Pointers from Functions

Task: Write a function get_max that takes an array of integers and its size as arguments, and returns a pointer to the maximum element in the array.

Solution:

C

int *get_max(int arr[], int size) {
    int *max_ptr = &arr[0];  // Assume the first element is the maximum
    for (int i = 1; i < size; i++) {
        if (arr[i] > *max_ptr) {
            max_ptr = &arr[i];
        }
    }
    return max_ptr;
}

Pointers and Arrays

6. Pointers as Arrays

Task: Access and print all elements of an array arr using pointer arithmetic.

Solution:

C

int arr[5] = {10, 20, 30, 40, 50};
int *ptr = arr;

for (int i = 0; i < 5; i++) {
    printf("%d ", *ptr);
    ptr++;
}

7. Arrays as Pointers

Task: Pass an array arr to a function print_array that prints all its elements.

Solution:

C

void print_array(int arr[], int size) {
    for (int i = 0; i < size; i++) {
        printf("%d ", arr[i]);
    }
}

Pointers and Strings

8. Accessing Characters in a String

Task: Print each character of a string str using pointer arithmetic.

Solution:

C

char str[] = "Hello, world!";
char *ptr = str;

while (*ptr != '\0') {
    printf("%c", *ptr);
    ptr++;
}

9. Modifying Strings Using Pointers

Task: Write a function to_upper that converts all lowercase characters in a string to uppercase.

Solution:

C

void to_upper(char str[]) {
    char *ptr = str;
    while (*ptr != '\0') {
        if (*ptr >= 'a' && *ptr <= 'z') {
            *ptr = *ptr - 'a' + 'A';  // Convert to uppercase
        }

CodeForHunger

Learn coding the easy way. Find programming guides, examples and solutions with explanations.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button