C Programming
Unit 8: Pointers
Pointer fundamentals, arithmetic, arrays, multidimensional arrays, strings, and dynamic memory allocation.
Introduction and importance of pointers
A pointer is a variable that stores the address of another variable. Pointers are important in C because they:
- Allow efficient access and manipulation of memory.
- Enable functions to modify variables passed to them (pass by reference).
- Are used to handle arrays, strings, and structures effectively.
- Are required for dynamic memory allocation.
Declaration:
data_type *pointer_name;
int *p; /* pointer to int */
float *f; /* pointer to float */
char *c; /* pointer to char */
Reference and dereference operator
- Reference operator (
&) — returns the address of a variable. - Dereference operator (
*) — accesses the value stored at a pointer’s address.
#include <stdio.h>
int main(void)
{
int a = 10;
int *p = &a; /* p holds address of a */
printf("Address of a : %p\n", (void *)&a);
printf("Value of p : %p\n", (void *)p);
printf("Value of *p : %d\n", *p); /* 10 */
*p = 20; /* modifies a through p */
printf("Value of a : %d\n", a); /* 20 */
return 0;
}
Pointer arithmetic
Arithmetic on pointers is performed in units of the pointed-to data type.
| Operation | Meaning |
|---|---|
p + 1 | Address of the next element |
p - 1 | Address of the previous element |
p1 - p2 | Number of elements between p1 and p2 |
++p, --p | Move to next / previous element |
#include <stdio.h>
int main(void)
{
int arr[3] = {10, 20, 30};
int *p = arr;
printf("%d\n", *p); /* 10 */
p++;
printf("%d\n", *p); /* 20 */
p++;
printf("%d\n", *p); /* 30 */
return 0;
}
Pointer and array
The name of an array acts as a pointer to its first element. arr[i] is equivalent to
*(arr + i).
#include <stdio.h>
int main(void)
{
int arr[5] = {1, 2, 3, 4, 5};
int *p = arr;
for (int i = 0; i < 5; i++)
printf("%d ", *(p + i)); /* 1 2 3 4 5 */
return 0;
}
Pointer with multidimensional array
In a 2D array, arr[i][j] is equivalent to *(*(arr + i) + j).
#include <stdio.h>
int main(void)
{
int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++)
printf("%d ", *(*(matrix + i) + j));
printf("\n");
}
return 0;
}
Pointer and strings
Strings can be accessed and manipulated using character pointers.
#include <stdio.h>
int main(void)
{
char *s = "Hello";
while (*s != '\0') {
printf("%c", *s);
s++;
}
printf("\n");
return 0;
}
Dynamic memory allocation
C provides functions in <stdlib.h> to allocate and release memory at runtime.
| Function | Purpose |
|---|---|
malloc(n) | Allocates n bytes; returns a pointer (memory uninitialized). |
calloc(n, size) | Allocates memory for n elements; initializes to 0. |
realloc(p, n) | Resizes the previously allocated block. |
free(p) | Releases previously allocated memory. |
Example — using malloc:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int n, *arr;
printf("Enter size: ");
scanf("%d", &n);
arr = (int *) malloc(n * sizeof(int));
if (arr == NULL) {
printf("Memory allocation failed\n");
return 1;
}
for (int i = 0; i < n; i++)
arr[i] = i + 1;
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
free(arr);
return 0;
}
Example — using calloc:
int *arr = (int *) calloc(5, sizeof(int));
/* All 5 elements initialized to 0 */
free(arr);