2. POINTERS
Definition
• variable that stores the memory address of another variable.
• directly refers to the location of a value in memory.
Used for
• dynamic memory allocation
• efficient manipulation of data.
• implementation of various advanced data structures and
algorithms.
3. BASICS OF POINTERS
• Syntax
Data_type *pointer_variable_name;
• Example:
Declaration
int *intPointer;
Initialization
int variable = 10;
int *intPointer = &variable;
• * -> Dereference operator.
4. POINTER ARITHMETIC
• manipulating the memory addresses stored in pointers.
• navigating through arrays, structures, and dynamic memory.
Increment (`++`) and Decrement (`--`).
• Example
• int numbers[] = {10, 20, 30, 40, 50};
• int *ptr = numbers; // Points to the first element of the array
• Pointer Increment:
• ptr++;
• Pointer Decrement
• ptr--;
5. PASSING POINTERS TO FUNCTIONS
• Syntax
Return_type function_name(data_type *parameter_name) {
//function body
}
• Example
Void modifyValue(int *ptr) {
*ptr = 20;
}
• Advantages
efficient memory usage
Direct Modification of Original Data
Reduced Overhead
Dynamic Memory Manipulation
6. RETURNING POINTERS FROM FUNCTIONS
• Syntax
Data_type* function_name() {
// Allocate memory and return a pointer to it
}
• Example
int* allocateAndReturn() {
int *ptr = (int*)malloc(sizeof(int));
// Additional logic or data initialization if needed
return ptr;
}
7. DYNAMIC MEMORY ALLOCATION
• Malloc (Memory Allocation)
• It is used to dynamically allocate a specified number of bytes of memory.
• Returns a pointer to the beginning of the allocated memory.
Example: Allocating memory for an integer
int *ptr = (int*)malloc(sizeof(int));
• Calloc (Contiguous Allocation)
• contiguous memory allocation and initializes the allocated memory to zero.
• Takes two arguments – the number of elements and the size of each element.
• Returns a pointer to the beginning of the allocated memory.
// Example: Allocating memory for an array of 5 integers
int *arr = (int*)calloc(5, sizeof(int));
8. • Free
• free is used to deallocate memory previously allocated using malloc or calloc.
• It takes a pointer to the memory block to be freed.
• After freeing memory, the pointer should not be used until reallocated.
// Example: Freeing dynamically allocated memory
free(ptr);
9. ARRAYS AND POINTERS
• Array Name as a Pointer:
int numbers[] = {10, 20, 30};
int *ptr = numbers; // Equivalent to &numbers[0]
• Pointer Arithmetic with Arrays & Array Indexing with Pointers:
int firstElement = *ptr; // 10
int secondElement = *(ptr + 1); // 20
• Advantages
Dynamic Array Sizes
Efficient Function Arguments
Flexibility in Memory Manipulation
10. FUNCTION POINTERS
• Definition
. Point to functions instead of data
. Call functions indirectly through the pointer.
• Declaration
int (*functionPtr)(int, int);
• Initialization
int add(int a, int b) {
return a + b;
}
int (*functionPtr)(int, int) = add;
11. REAL WORLD EXAMPLES OF POINTERS IN FUNCTIONS
• Dynamic Memory Allocation
malloc, calloc, and realloc return pointers to dynamically allocated memory.
• String Manipulation
strcpy, strcat, and strlen use pointers to manipulate strings efficiently.
• Sorting Algorithms
Quicksort or mergesort often use function pointers
• Function Pointers in API Libraries:
APIs often use function pointers to define plugin interfaces
• Memory Management in Data Structures
Data structures like linked lists, trees, and graphs often use function pointers
12. EXAMPLE PROGRAM
#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 swapping: x = %d, y = %dn”, x, y);
swap(&x, &y);
printf(“After swapping: x = %d, y = %dn”, x, y);
return 0;
}