Mastering Pointers in C/C++: 10 Practical Coding Exercises for Beginners
If you are diving into C or C++ programming, there is one concept you simply cannot escape: Pointers. Often considered the first major hurdle for beginners, pointers are actually incredibly powerful tools that give you direct access to memory and help you write highly efficient code.
Before jumping into the exercises below, if you need a quick conceptual refresher, I highly recommend checking out some classic guides such as [Pointers in C Programming: What is Pointer, Types & Examples] or an [Introduction to C Pointers].
To help you solidifying your understanding, here are 10 practical pointer exercises, complete with reference solutions. Let's code!
Exercise 1: Basic Pointer Syntax
Goal: Design a C program to demonstrate the foundational syntax of pointers, including pointer declaration, address-of retrieval (&), and dereferencing (*).
Reference Solution:
- /* Pointer Basic Syntax
- Author: Holan */
- #include <stdio.h>
- #include <stdlib.h>
- int main() {
- int n = 50; // Variable declaration
- int *ip; // Pointer declaration
-
- ip = &n; // Assigning the address of 'n' to pointer 'ip'
-
- printf("The address of n (&n): %X\n", &n);
- printf("The value of n: %i\n", n);
- printf("The address of ip (&ip): %X\n", &ip);
- printf("The value stored in ip (address of n): %X\n", ip);
- printf("The value decompressed/dereferenced (*ip): %i\n", *ip);
-
- return 0;
- }
Exercise 2: Dynamic Memory Allocation
Goal: Write a C program that dynamically allocates memory for an array of integers based on user input using malloc(), and make sure to release it properly using free().
Reference Solution:
- /* Dynamic memory allocation
- Author: Holan */
- #include <stdio.h>
- #include <stdlib.h>
- int main() {
- int n;
- int *pI;
-
- printf("How many integers? ");
- scanf("%d", &n);
-
- // Allocating memory dynamically using malloc
- pI = (int *) malloc(n * sizeof(int));
-
- if(pI == NULL) // Error handling if allocation fails
- {
- printf("Couldn't allocate memory\n");
- return 0; // Exit the program
- }
-
- printf("Memory successfully allocated.\n");
-
- // Always remember to free the memory allocated by malloc
- free(pI);
- return 0;
- }
Exercise 3: Multiplying Two Numbers Using Pointers
Goal: Create a simple program that takes two numbers from the user and multiplies them together. The catch? You must perform the math using pointers!
Reference Solution:
Exercise 4: Navigating Arrays with Pointers
Goal: In C, arrays and pointers are closely related. Write a program that uses a pointer to access and print every element of an integer array.
Reference Solution:
Exercise 5: Pointer Arithmetic (Increment and Decrement)
Goal: In Exercise 4, we accessed elements by adding an integer to the pointer (*(pI + i)). For this exercise, achieve the exact same behavior, but use the increment (++) and decrement (--) operators instead.
(Tip: If you want to know more about the underlying mechanics, search for tutorials on "How pointer arithmetic works".)
Reference Solution:
Exercise 6: Calculating String Length
Goal: Strings in C are character arrays terminated by a null character ('\0'). Write a program that calculates the exact length of a user-inputted string using pointer traversal.
Reference Solution:
Exercise 7: Swapping Two Numbers (Call by Reference)
Goal: Create a user-defined function that swaps the values of two variables. You must pass the variables by reference using pointers so the changes persist outside the function.
Reference Solution:
Exercise 8: Manual String Concatenation
Goal: Without using built-in string functions like strcat(), write a program that uses pointers to combine two separate strings into a single target buffer.
Reference Solution:
Exercise 9: Handling the Null Pointer
Goal: A null pointer points to nowhere. Attempting to dereference it will crash your program. Write a program that demonstrates how to safely check for NULL before accessing pointer data.
Reference Solution:
Exercise 10: Function Pointers
Goal: Pointers don't just point to data; they can also point to functions! Create basic arithmetic functions (add, subtract, multiply, divide) and execute them dynamically through a single function pointer.
Reference Solution:
Wrapping Up
Pointers might feel intimidating at first, but once you master how addresses and variables interact in memory, they become an invaluable asset in your programming toolkit.
If you found these exercises helpful, please consider supporting the blog by clicking the ads or buying me a coffee. Happy coding!
留言