Handouts Module 5 Pointers and Files Under Construction Vs
Handouts Module 5 Pointers and Files Under Construction Vs
POINTERS
Pointers are a fundamental feature of the C programming language, allowing for low-
level memory access and manipulation. A pointer is a variable that stores the memory
address of another variable, function, or even another pointer. This capability enables
dynamic memory allocation, efficient array handling, and the creation of complex data
structures like linked lists and trees.
Dereferencing
Dereferencing a pointer means accessing the value stored at the memory address it points to.
This is done using the asterisk (*) operator.
int var = 10;
int *ptr = &var; // Pointer initialized with the address of 'var'
int value = *ptr; // Dereferencing pointer to get the value (value = 10)
Example:
Types of Pointers
Pointers in C can be classified into several types based on the data they point to:
Pointers Declaration
int *intPtr;
Integer Pointers: Point to integer values.
Array Pointers: Point to the first element of an array. char *arrPtr = array_name;
Pointer Arithmetic
You can perform a limited number of arithmetic operations on pointers. These operations
are:
Increment and decrement
Addition and subtraction
Comparison
Assignment
The increment (++) operator increases the value of a pointer by the size of the
data object the pointer refers to. For example, if the pointer refers to the second
element in an array, the ++ makes the pointer refer to the third element in the
array.
You can compare two pointers with the following operators: ==, !=, <, >, <=,
and >=.
ARITHMATIC OPERATION INCREMENT Pointers in Array operations
(increment in pointer)
O/P: 1 2 3 4 5
INPUT 3
1
2
3
=6
Passing Pointers to Functions // Add two numbers using a function
(Pass by Reference) and pointer
Pointers can be passed to functions
to modify the original data directly.
This is an efficient way to avoid
passing large data structures.
1 #include<stdio.h>
int addition ();
int main ()
{
int result;
int (*ptr)();
ptr = &addition;
result = (*ptr)();
printf("The sum is %d",result);
}
int addition()
{
int a, b;
printf("Enter two numbers?");
scanf("%d %d",&a,&b);
return a+b;
}
It's a good practice to initialize pointers to NULL when they are declared, especially
if they are not immediately assigned valid addresses.
Initializing pointers with NULL helps in avoiding undefined behavior or
segmentation faults.
int *ptr = NULL;
NULL is returned by functions that fail to allocate memory dynamically using
functions like malloc() or calloc().
int *ptr = malloc(sizeof(int));
if (ptr == NULL) {
// Memory allocation failed
}
#include <stdio.h>
#include <stdlib.h>
int main()
{ int* ptr;
// Dynamically allocate memory using malloc()
ptr = (int*)malloc(5 * sizeof(int));
// Check if the memory has been successfully allocated or not
if (ptr == NULL) {
printf("Memory not allocated.\n");
exit(0); }
else {
printf("Memory successfully allocated using malloc.\n");
for (i = 0; i < 5; i++) {
ptr[i] = i + 1; }
printf("The elements of the array are: ");
for (i = 0; i < 5; ++i) {
printf("%d, ", ptr[i]); }
}return 0;
}
Dangling Pointer
A dangling pointer is a pointer that continues to point to a memory location that
has been deallocated.
free(ptr);
ptr = NULL; // Prevents 'ptr' from becoming a dangling pointer