Programming Essentials in C - Lectures 9
What is a Pointer
A pointer in C is a derived data type used to store the address of another variable.
It allows access and modification of data located in memory. Pointers are essential for:
- Efficient data passing between functions
- Creating dynamic data structures (linked lists, trees, graphs)
Pointer Declaration
To declare a pointer, use the dereferencing operator (*) with a valid C data type:
Syntax:type *varname
typeis the base type of the pointervarnameis the name of the pointer
Note: The * in the declaration is not multiplication—it signifies that the variable is a pointer.
Example:
int *intPtr; // integer pointer
float *floatPtr; // float pointer
char *charPtr; // char pointerPointer Initialization
A pointer is initialized using the address-of operator (&) to assign it the address of a variable.
Example:
int var = 5;
// stores the address of `var` in memory
int *ptr = &var;
// dereference the pointer to access its value
printf("%d => %d\n", var, *ptr); // 5 => 5Referencing and Dereferencing Pointers
&(Address-of Operator): Used to get the address of a variable.*(Dereference Operator): Used to access the value at the memory address held by a pointer.
Access and Manipulate Values Using Pointers
You can modify the value of a variable via its pointer.
int var = 5;
int *ptr = &var;
printf("%d => %d\n", var, *ptr); // 5 => 5
*ptr = 10; // modifies both `ptr` and `var`
printf("%d => %d\n", var, *ptr); // 10 => 10Printing the address of a variable:
int var = 5;
int *ptr = &var;
printf("value of &var: %p\n", &var); // using %p for pointers
printf("value of ptr: %p\n", ptr); // prints the same addressNOTE
The type of a variable and its pointer must match.
Output:
value of &var: 0x7ffd5cbbef94
value of ptr: 0x7ffd5cbbef94Pointer to a Pointer
A pointer can also store the address of another pointer.
int var = 5; // integer variable
int* ptr = &var; // integer pointer to a variable
int** ptrToptr = &ptr; // integer pointer to a pointerPointer Arithmetic in C
Increment and Decrement of a Pointer
Incrementing a pointer moves it to the next memory location based on the data type size.
int x = 10;
int *y = &x;
printf("Value of y before increment: %d\n", y);
y++;
printf("Value of y after increment: %d\n", y);Output:
Pointer y increases by sizeof(int) (usually 4 bytes).
Notice that the address has changed (0x7ff...44 -> 0x7ff...48).
Value of y before increment: 0x7ffc32820844
Value of y after increment: 0x7ffc32820848NOTE
Memory addresses change with each run.
Decrementing a pointer in an array
int arr[] = { 10, 20, 30 };
int* ptr = &arr[2];
for (int i = 0; i < 3; i++) {
printf("%d ", *ptr); // print the current value
ptr--; // move the pointer to the previous element
}Addition and Subtraction of Integer to Pointer
Adding or subtracting an integer to a pointer moves it forward or backward in memory.
int arr[] = { 10, 20, 30, 40, 50 };
int* ptr = arr; // points to arr[0]
for (int i = 0; i < 5; i++) {
// move the pointer by `i` positions in memory then print its value
printf("%d ", *(ptr + i));
}Summary
| Expression | Description |
|---|---|
ptr + n | Move forward to the |
ptr - n | Move backward to the |
*(ptr + n) | Value at the |
*(ptr - n) | Value at the |
Reverse Printing a String Using Pointer
#include <stdio.h>
#include <string.h> // for strlen
int main() {
char str[] = "Pointer";
char* ptr = &str[strlen(str) - 1];
printf("Reversed string: ");
while (ptr >= str) {
printf("%c", *ptr);
ptr--;
}
printf("\n");
return 0;
}Output:
Reversed string: retnioPPointer to an Array
A pointer to an array points to the whole array, not just its first element.
int arr[5] = { 10, 20, 30, 40, 50 };
int (*ptr)[5] = &arr; // Pointer to the entire array
for (int i = 0; i < 5; i++) {
printf("%d ", (*ptr)[i]);
}