📌 C Pointers Intro – What on Earth is a Pointer?
If you’re thinking a pointer is just a fancy way to say “go here” — you’re not wrong! In C, a pointer is a variable that stores the memory address of another variable. Sounds scary? Don’t worry — by the end of this, you’ll point like a pro 🧭
🔹 Why Use Pointers?
- Access and manipulate memory directly
- Efficiently pass large data (like arrays) to functions
- Dynamic memory allocation (malloc, calloc, etc.)
📍 Pointer Syntax
Here’s how we declare a pointer:
int *ptr; // pointer to an int
📝 Example: Basic Pointer
Let’s declare a pointer, store the address of a variable, and access its value using the pointer.
#include <stdio.h>
int main() {
int x = 10;
int *ptr = &x; // ptr stores the address of x
printf("Value of x: %d\n", x);
printf("Address of x: %p\n", &x);
printf("Pointer ptr stores: %p\n", ptr);
printf("Value pointed to by ptr: %d\n", *ptr); // dereference
return 0;
}
🔍 Let’s Break it Down
&x: “Address of x”int *ptr: Declaring a pointer to an int*ptr: Dereferencing pointer — gives the value at the address
🚨 Don’t Confuse:
*ptr— dereference, access value&var— get address of variable
🧠 Fun Fact
Pointer arithmetic lets you move through memory like a ninja 🥷. But don’t worry — we’ll get there soon.
📚 Summary
- A pointer stores the memory address of a variable
- Use
&to get the address, and*to access the value - They’re powerful, but must be used with care!
🎯 Practice Time
Try creating pointers to float, char, and double variables. Use printf to print their addresses and values!