Pointers 1
Pointers 1
Subtopics
• Introduction to Pointers
• Syntax
• Types of Pointer
• Uses of pointers
• Applications of Pointers
• Pointer Arithmetic
• Dynamic Memory Allocation
• Common Pointer Errors
• Conclusion
Definition of Pointer
A pointer is a variable that stores the
memory address of another variable. Instead of
holding actual data, a pointer holds the location
where the data is stored in memory. This allows
programs to directly access and manipulate data in
memory.
Key Points:
2.Indirect Access:
•Through pointers, you can indirectly access or modify the
value of the variable they point to using the dereference
operator (*).
3.Dynamic Behavior:
•Pointers enable dynamic memory allocation, passing
variables by reference, and creating complex data
structures like linked lists, trees, etc.
Pointer Syntax
Declaration:
syntax: data_type *pointer_name;
Example: int *ptr;
Initialization:
syntax: pointer_name = &variable;
Example:
int x = 10;
int *ptr = &x; // ptr now holds the address of x
Dereferencing:
Access the value stored at the memory address using *.
Example:
printf("%d", *ptr); // Outputs the value of x
Uses Of Pointers
Key Uses:
• Dynamic Memory
Management: Allocating and
freeing memory.
• Function Arguments: Pass-by-
reference for efficiency.
• Data Structures: Building linked
lists, trees, and graphs.
• System Programming: Direct
memory manipulation.
• Efficiency: Reduced overhead in
accessing data.
Types Of Pointers
Null Pointer:
Example Code:
Void Pointer:
• A generic pointer of type void * that can store the address of any
data type but cannot be dereferenced directly.
Example:
int a = 10;
void *ptr = &a; // Generic pointer
printf("Value: %d\n", *(int *)ptr); // Typecast required
Function Pointer:
A pointer that holds the address of a function, enabling
functions to be passed as arguments or called indirectly.
Syntax:
return_type (*function_pointer)(parameter_list);
Example Code:
int add(int a, int b) {
return a + b;
}
int (*func_ptr)(int, int) = add; // Pointer to add()
printf("Sum: %d\n", func_ptr(5, 10)); // Indirect function call
Double Pointer:
A pointer that stores the address of another pointer,
effectively creating a chain of pointers.
Syntax:
data_type **pointer_name;
Example Code:
int x = 10;
int *p = &x;
int **pp = &p;
printf("Value of x: %d\n", **pp); // Accessing x via double pointer
Applications of Pointers
Dynamic Memory Allocation:
Example:
Increment (++)
•Moves the pointer to the next memory location.
•For example, if a pointer points to an integer (int), it moves by sizeof(int) bytes.
Decrement(--)
Moves the pointer to the previous memory location.
Subtraction (-)
Types:
• Malloc()
• Calloc()
• Realloc()
• Free()
malloc (Memory Allocation)
• The memory is uninitialized (contains garbage values).
• Returns a pointer to the first byte of the allocated memory or NULL if allocation
fails.
• Allocates a specified number of bytes of memory.
Syntax:
Example:
int* ptr = (int*) malloc(5 * sizeof(int));
Syntax:
Syntax:
void* realloc(void* ptr, size_t size);
Example:
ptr = (int*) realloc(ptr, 10 * sizeof(int));
free (Deallocate Memory)
• Frees memory allocated by malloc, calloc, or realloc.
• Prevents memory leaks.
Syntax:
void free(void* ptr);
Example:
free(ptr);
Program
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr;
ptr = (int *)malloc(sizeof(int));
// Check if memory allocation was successful
if (ptr == NULL) {
printf("Memory allocation failed.\n");
return 1;
}
// Assign and print a value
*ptr = 42;
printf("Value stored in dynamically allocated memory: %d\n", *ptr);
// Free the memory
free(ptr);
return 0;
} Output:
Common Pointer
Pointers are a Errors
powerful feature in C and C++, but improper
handling can lead to significant issues in the program. Here are
some of the most common pointer errors, along with their causes,
explanations, and ways to avoid them.
Dereferencing a Null Pointer
A null pointer is a pointer that doesn't point to any valid memory
address. Dereferencing a null pointer occurs when you try to access
memory using a null pointer, which leads to undefined behavior and
usually results in a segmentation fault or crash.
Cause
A pointer is initialized to NULL or nullptr and is later dereferenced
without being assigned a valid memory address.
int *ptr = NULL;
printf("%d", *ptr);
Conclusion