
Memory Address in C
? Memory Address in C
In C, every variable is stored in memory, and that memory location has a unique address. You can access this address using the address-of operator &
.
? What is a Memory Address?
It’s the location in memory (RAM) where a variable is stored. Think of it as a "house number" for your variable.
? Example: Getting Memory Address
#include <stdio.h>int main() { int num = 42; printf("Value: %d\n", num); printf("Address: %p\n", &num); // %p = format for memory address return 0;}
Output:
Value: 42Address: 0x7ffee44d66a4 // (your address will differ)
? Key Concepts
Operator | Meaning |
---|---|
&var | Address of variable var |
*ptr | Value at address stored in ptr (dereferencing) |
? Example: Pointer and Address
int num = 100;int *ptr = #printf("num = %d\n", num); // prints valueprintf("&num = %p\n", &num); // prints addressprintf("ptr = %p\n", ptr); // prints address stored in ptrprintf("*ptr = %d\n", *ptr); // prints value at that address
? Why Care About Memory Addresses?
Needed for pointers
Essential in arrays, functions, and dynamic memory
Useful for low-level programming (like embedded or system programming)