Pointers
Pointers are a fundamental and powerful feature in C that let you work directly with memory. They can seem a bit tricky at first, but they’re incredibly useful once you get the hang of them.
What is a Pointer?
int *ptr; // Pointer to an integerint x = 10; int *ptr = &x; // 'ptr' now holds the address of 'x'int value = *ptr; // Retrieves the value at the address stored in 'ptr'
Pointer Example
#include <stdio.h>
int main() {
int x = 10; // Regular integer variable
int *ptr = &x; // Pointer variable that holds the address of 'x'
printf("Value of x: %d\n", x); // Outputs: Value of x: 10
printf("Address of x: %p\n", &x); // Outputs the memory address of 'x'
printf("Value of ptr: %p\n", ptr); // Outputs the address stored in 'ptr'
printf("Value pointed to by ptr: %d\n", *ptr); // Outputs: Value pointed to by ptr: 10
return 0;
}Pointer Arithmetic
Dynamic Memory Allocation
Pointers and Functions
Pointers to Pointers
Summary
Last updated