Practical Applications of Pointer Manipulation in C Programming
Fundamental Pointer Operations and Memory Addressing
Pointers store memory addresses, allowing indirect access and modification of variables. The following implementation demonstrates pointer initialization, address assignment, dereferencing, and hexadecimal address printing.
#include <stdio.h>
int main(void) {
int integer_var, target_val = 3;
float decimal_var;
int *int_ptr = &integer_var;
float *float_ptr = &decimal_var;
printf("Enter integer and floating-point values: ");
scanf("%d %f", int_ptr, float_ptr);
printf("\nDirect Access: %d, %.2f\n", integer_var, decimal_var);
printf("Pointer Dereference: %d, %.2f\n", *int_ptr, *float_ptr);
printf("Memory Addresses (Direct): %p, %p\n", (void*)&integer_var, (void*)&decimal_var);
printf("Memory Addresses (Pointers): %p, %p\n", (void*)int_ptr, (void*)float_ptr);
int_ptr = &target_val;
printf("\nReassigned Pointer Value: %d\n", *int_ptr);
printf("Target Address Comparison: %p, %p\n", (void*)int_ptr, (void*)&target_val);
return 0;
}
Technical Note: When assigning an address to a pointer, the & operator retrieves the variable's location. Dereferencing via * accesses the stored value. Reassigning a pointer chenges its target without altering the original data.
Pass-by-Value versus Pass-by-Reference for Variable Swapping
Modifying caller-scope variables requires passing their memory addresses. Passing values by copy restricts modifications to the local function stack.
#include <stdio.h>
void swap_by_copy(int lhs, int rhs);
void swap_by_reference(int *lhs, int *rhs);
int main(void) {
int num_one, num_two;
printf("Input first integer: ");
scanf("%d", &num_one);
printf("Input second integer: ");
scanf("%d", &num_two);
swap_by_copy(num_one, num_two);
printf("After copy-based swap: %d, %d\n", num_one, num_two);
swap_by_reference(&num_one, &num_two);
printf("After reference-based swap: %d, %d\n", num_one, num_two);
return 0;
}
void swap_by_copy(int a, int b) {
int holder = a;
a = b;
b = holder;
}
void swap_by_reference(int *x, int *y) {
int temp = *x;
*x = *y;
*y = temp;
}
Techniacl Note: The first function operates on local copies, leaving the original variables unchanged. The second function accepts memory addresses, using the dereference operator * to modify the actual memory locations. When invoking pointer parameters, the & operator must be used to pass the target's adress.
String Reversal and Concatenation Using Pointer Arithmetic
Strings in C are null-terminated character arrays. Pointer traversal enables in-place reversal and memory-efficient concatenation without auxiliary buffers.
#include <stdio.h>
char* invert_sequence(char *buffer);
char* merge_strings(char *dest, char *src);
int main(void) {
char primary_buf[64], secondary_buf[64];
char *result_ptr;
printf("Enter string to reverse: ");
scanf("%63s", primary_buf);
result_ptr = invert_sequence(primary_buf);
printf("Reversed output: %s\n", result_ptr);
printf("Enter first concatenation string: ");
scanf("%63s", primary_buf);
printf("Enter second concatenation string: ");
scanf("%63s", secondary_buf);
result_ptr = merge_strings(primary_buf, secondary_buf);
printf("Concatenated result: %s\n", result_ptr);
return 0;
}
char* invert_sequence(char *text) {
char *start_ptr = text;
char *end_ptr = text;
while (*end_ptr != '\0') {
end_ptr++;
}
end_ptr--;
while (start_ptr < end_ptr) {
char swap_char = *start_ptr;
*start_ptr = *end_ptr;
*end_ptr = swap_char;
start_ptr++;
end_ptr--;
}
return text;
}
char* merge_strings(char *base, char *append) {
char *write_ptr = base;
char *read_ptr = append;
while (*write_ptr != '\0') {
write_ptr++;
}
while (*read_ptr != '\0') {
*write_ptr = *read_ptr;
write_ptr++;
read_ptr++;
}
*write_ptr = '\0';
return base;
}
Technical Note: Reversing a string requires a bidirectional traversal, swapping characters until the pointers cross. Concatenation involves advancing a write pointer to the terminating null byte, then copying the source sequence character by character. Returning the original base pointer ensures the modified string remains accessible to the caller.
Two-Pointer Partitioning for Parity-Based Array Sorting
Rearranging array elements based on parity can be optimized using a dual-pointer approach. This technique operates in-place with linear time complexity.
#include <stdio.h>
#define ARRAY_SIZE 10
void partition_parity(int *dataset, size_t count);
int main(void) {
int data_set[ARRAY_SIZE];
size_t i;
printf("Enter %d integers: ", ARRAY_SIZE);
for (i = 0; i < ARRAY_SIZE; i++) {
scanf("%d", &data_set[i]);
}
partition_parity(data_set, ARRAY_SIZE);
printf("Partitioned array: ");
for (i = 0; i < ARRAY_SIZE; i++) {
printf("%d ", data_set[i]);
}
printf("\n");
return 0;
}
void partition_parity(int *arr, size_t len) {
int *left_cursor = arr;
int *right_cursor = arr + len - 1;
while (left_cursor < right_cursor) {
while (left_cursor < right_cursor && (*left_cursor % 2) != 0) {
left_cursor++;
}
while (left_cursor < right_cursor && (*right_cursor % 2) == 0) {
right_cursor--;
}
if (left_cursor < right_cursor) {
int temp_val = *left_cursor;
*left_cursor = *right_cursor;
*right_cursor = temp_val;
left_cursor++;
right_cursor--;
}
}
}
Technical Note: The algorithm maintains two cursors starting at opposite ends. The left cursor advances while encountering odd numbers, and the right cursor retreats while encountering even numbers. When both halt on misplaced elements, a swap occurs. Boundary checks prevent pointer crossover, ensuring all odd values shift to the left partition and evens to the right without allocating additional memory.