Pointers in C
A pointer in C is a variable that stores the memory address of another variable. Instead of storing a value directly, a pointer holds the location where the value is stored.
For example, imagine a house where various items (values) are stored. The house address represents the memory location, and the items inside represent the stored data. Just like you use an address to locate a house, a pointer in C is used to access a value stored at a specific memory location.
Syntax:
Example 1:
#include <stdio.h> int main() { int num = 10; // A normal integer variable int *ptr; // Pointer declaration ptr = # // Assign address of num to ptr printf("Value of num: %d\n", num); printf("Address of num: %p\n", &num); printf("Pointer stores address: %p\n", ptr); printf("Value pointed by ptr: %d\n", *ptr); // Dereferencing the pointer return 0; }
Output:
Value of num: 10 Address of num: 0x7ffd7f20e5d4 Pointer stores address: 0x7ffd7f20e5d4 Value pointed by ptr: 10
We use %p in printf to print memory addresses because %p is specifically designed to format and display pointer (address) values. Addresses are large values (often 8 bytes on 64-bit systems), and using %p ensures they are displayed correctly.
Explanation:
The format 0x7ffd7f20e5d4 is a memory address in hexadecimal notation. Let's break it down:
1. 0x Prefix:
✰ This indicates that the number is in hexadecimal (base 16) format.
✰ Hexadecimal uses digits 0-9 and letters A-F to represent values.
2. 7ffd7f20e5d4 (Hexadecimal Number):
✰ This is the actual memory address where the variable (or pointer) is stored.
✰ It is a virtual memory address assigned by the operating system.
✰ On a 64-bit system, addresses are typically 12-16 hex digits long.
✰ On a 32-bit system, addresses are usually 8 hex digits long.
Example Interpretation:
If you convert 0x7ffd7f20e5d4 to decimal, you get a large number, which is the actual memory location in RAM.
Memory Address Breakdown:
✰ 7ffd → Could indicate a stack memory segment (depends on OS).
✰ 7f20e5d4 → The specific location within that segment.
Key Takeaways:
✰ This address is dynamically assigned and varies each time the program runs.
✰ Addresses in heap and stack regions may look different (stack addresses are often close together).
✰ If Address Space Layout Randomization (ASLR) is enabled, the address may change every execution for security reasons.
Example 2: Swapping Two numbers using Pointers.
#include <stdio.h> int main() { int a = 10, b = 20; int *num1 = &a, *num2 = &b; // Pointers storing the addresses of a and b printf("Before Swap: a = %d, b = %d\n", a, b); // Swapping using pointers int temp = *num1; *num1 = *num2; *num2 = temp; printf("After Swap: a = %d, b = %d\n", a, b); return 0; }
Output:
Before swapping: num1 = 10, num2 = 20 After swapping: num1 = 20, num2 = 10
Example 3: Swapping Two numbers without using third variable in Pointers.
#include <stdio.h> int main() { int a = 5, b = 15; int *p1 = &a, *p2 = &b; // Pointers to a and b printf("Before Swap: a = %d, b = %d\n", a, b); // Swapping using arithmetic operations with pointers *p1 = *p1 + *p2; *p2 = *p1 - *p2; *p1 = *p1 - *p2; printf("After Swap: a = %d, b = %d\n", a, b); return 0; }
Output:
Before Swap: a = 5, b = 15 After Swap: a = 15, b = 5