
Pointers Programming Practice
"Stack was the first structures that programmers used for maintaining data in orderly fashion"
— Try it!
Beginner Problems
- Variable and Address: Write a program to declare an integer variable, assign it a value, and then print the variable's value and its memory address.
- Dereferencing: Declare a pointer and make it point to a variable. Print the value of the variable using both the variable name and by dereferencing the pointer.
- Pointer Declaration: Declare pointers for different data types (int, char, float) and print their sizes using sizeof().
- Null Pointers: Declare a pointer and initialize it to NULL. Write an if condition to check if the pointer is null before trying to dereference it.
Intermediate problems
- Swapping Numbers: Write a function that takes two integer pointers as arguments and swaps the values of the variables they point to. This demonstrates call by reference.
- Pointer to Pointer: Declare a pointer that points to another pointer (int **p). Initialize it and use it to access the value of the original variable it points to. Draw a diagram to explain the relationship.
- Explaining Pointer Types:
- What is a dangling pointer? Write a code snippet that creates one by freeing memory that a pointer points to.
- What is a void pointer? Show how you can assign the address of an int to a void pointer and then typecast it back to an int* to print its value.
Pointer Arithmetic
- Incrementing a Pointer: Declare an int pointer to the first element of an array. Use a loop and the
++ operator on the pointer to iterate through the array and print each element.
// Example: int arr[] = {10, 20, 30, 40}; int *ptr = arr; for (int i = 0; i < 4; i++) { printf("%d ", *ptr); ptr++; }
- Pointer Scaling: Explain why incrementing an int pointer moves the address by 4 bytes (on most systems) while incrementing a char pointer moves it by only 1 byte. Reference the formula:
new_address = current_address + (number * sizeof(data type))
Pointer Arithmetic
- Incrementing a Pointer: Declare an int pointer to the first element of an array. Use a loop and the
++ operator on the pointer to iterate through the array and print each element.
// Example: int arr[] = {10, 20, 30, 40}; int *ptr = arr; for (int i = 0; i < 4; i++) { printf("%d ", *ptr); ptr++; }
- Pointer Scaling: Explain why incrementing an int pointer moves the address by 4 bytes (on most systems) while incrementing a char pointer moves it by only 1 byte. Reference the formula:
new_address = current_address + (number * sizeof(data type))
- Reverse a String: Write a function that takes a char* (a string) and reverses it in place using two pointers (one at the beginning, one at the end).
- Pointer Differencing: Create two pointers that point to elements in the same array. Calculate the difference between them to find out how many elements are between them.
- Pointer Comparison: Write a loop that uses pointer comparison (p < end_ptr) to iterate through an array instead of using an integer index.
Pointers with Arrays and Functions
- Array Name as a Pointer: Demonstrate that the name of an array acts as a pointer to its first element by printing the address of arr and &arr[0].
- Accessing Elements: Access and print array elements using both subscript notation (arr[i]) and pointer notation *(arr + i). Explain why they are equivalent.
- Passing Arrays to Functions: Write a function sumArray(int* arr, int size) that takes a pointer to an array and its size, and returns the sum of its elements. Call this function from main.
- Array of Pointers: Declare an array of char pointers (char* names[3]). Assign string literals to each pointer (e.g., "Alice", "Bob", "Charlie") and then loop through the array to print each name.
- Find Max/Min: Write a function that accepts an integer array and its size, and uses pointers to find the maximum and minimum elements in the array. Return these values back to main using pointer arguments (call by reference).
- 2D Arrays and Pointers: Pass a 2D array to a function and print its elements using pointer notation.
Dynamic Memory Allocation
- malloc(): Ask the user for a number n. Dynamically allocate an array of n integers using malloc(). Fill it with values and print them. Finally, release the memory using free().
- Error Checking: Modify the previous program to check if malloc() returned NULL. If it did, print an error message and exit.
- calloc(): Use calloc() to allocate a dynamic array. Show that calloc() initializes the memory to zero by printing the array elements immediately after allocation.
- realloc(): Create a dynamic array for 5 integers using malloc(). Then, use realloc() to expand its size to hold 10 integers. Keep the original 5 values and add 5 new ones.
- Dynamic 2D Array: Write a program to create a 2D array (matrix) of size rows × cols dynamically using a pointer to a pointer (
int **arr
). - Memory Leak: Explain what a memory leak is. Write a small program that causes a memory leak by allocating memory in a loop without freeing it.
Structures and Unions
- Define a Structure: Define a
struct
calledBook
with memberstitle
(char array),author
(char array), andprice
(float). Create aBook
variable, initialize its members, and print them using the dot (.
) operator. - Pointer to Structure: Create a pointer to the
Book
structure. Access and print the members using the arrow (->
) operator. - Unions: Define a
union
calledData
that can hold anint
, afloat
, or achar
. Demonstrate that all members share the same memory location by assigning a value to the integer member and then printing the character member. - Size of Struct vs. Union: Print the
sizeof
astruct
and aunion
with the same members. Explain why their sizes are different. - Array of Structures: Create an array of 5
Book
structures. Write a program to get input for all 5 books from the user and then print the details of the book with the highest price. - Dynamic Array of Structures: Modify the previous problem to dynamically allocate an array of n Book structures, where n is entered by the user.
- Passing Structures to Functions: Write a function that takes a
Book
structure as an argument (by value) and prints its details. Write another function that takes a pointer to aBook
structure (by reference) and modifies its price.