Pointers – Introduction
Pointers – परिचय
What is a Pointer?
A pointer is a variable that stores the memory address of another variable. Instead of holding a data value (like 42 or 3.14), it holds the location in memory where a value is stored.
Every variable you create occupies some memory. That memory has an address (a number). A pointer stores that address.
int x = 42; // x stores value 42, at some address like 0x1234 int *p = &x; // p stores the ADDRESS of x (0x1234) // ↑ means "pointer to int" // ↑ means "address of x"
Why use pointers? They allow:
- Pass by reference — modify variables in other functions
- Dynamic memory — allocate memory at runtime (malloc, free)
- Arrays and strings — efficient array manipulation
- Data structures — linked lists, trees, graphs
Declaring and Initializing Pointers
int *ip; // Pointer to int float *fp; // Pointer to float char *cp; // Pointer to char int *np = NULL; // NULL pointer — points to nothing (safe!) // Good initialization: int age = 25; int *ptr = &age; // ptr points to age
An uninitialized pointer contains a random (garbage) address. Dereferencing it causes undefined behavior — program crash! Always initialize to NULL or a valid address before use.
& and * Operators
| Operator | Name | Usage | Returns |
|---|---|---|---|
& | Address-of | &variable | Memory address of variable |
* | Dereference (Indirection) | *pointer | Value at the address stored in pointer |
#include <stdio.h> int main() { int x = 42; int *p = &x; // p points to x printf("Value of x = %d\\n", x); // 42 printf("Address of x = %p\\n", &x); // 0x... (hex address) printf("Value of p = %p\\n", p); // Same as &x printf("*p (value) = %d\\n", *p); // 42 (dereferencing) *p = 100; // Change x through pointer! printf("x after *p=100: %d\\n", x); // x is now 100! return 0; }
Pointer Arithmetic
You can add or subtract integers from pointers. The pointer moves by the size of its type.
int arr[] = {10, 20, 30, 40, 50}; int *p = arr; // Points to first element printf("%d\\n", *p); // 10 p++; // Move to next int (+4 bytes) printf("%d\\n", *p); // 20 p += 2; // Move 2 ints forward (+8 bytes) printf("%d\\n", *p); // 40
Pointers in Functions – Pass by Reference
#include <stdio.h> void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; } int main() { int x = 10, y = 20; printf("Before: x=%d, y=%d\\n", x, y); swap(&x, &y); // Pass addresses printf("After: x=%d, y=%d\\n", x, y); return 0; }
Pointers and Arrays
An array name itself is a pointer to its first element!
int arr[] = {1, 2, 3}; // These are equivalent: printf("%d\\n", arr[2]); // Array notation printf("%d\\n", *(arr + 2)); // Pointer notation
Summary
- A pointer stores the memory address of a variable
- Declare with
*:int *p; &variable— gets address of variable*pointer— gets value at the address (dereference)- Always initialize pointers before use; use
NULLas safe default - Pointer arithmetic moves by the size of the pointed type
- Array name = pointer to first element
- Pass pointers to functions to modify variables (pass by reference)
Write programs: (1) Print address and value of 5 variables (2) Swap two numbers using pointers (3) Traverse array using pointer arithmetic (4) Function that returns min and max of array via pointer params.
Pointer क्या है?
Pointer एक variable है जो किसी दूसरे variable का memory address store करता है। Data value (जैसे 42) की बजाय, यह memory में उस location को hold करता है जहाँ value store है।
int x = 42; // x value 42 store करता है, address 0x1234 पर int *p = &x; // p, x का ADDRESS (0x1234) store करता है // * = "pointer to int" declaration में // & = "address of x"
Pointers क्यों? ये allow करते हैं:
- Pass by reference — दूसरे functions में variables modify करना
- Dynamic memory — runtime पर memory allocate करना
- Arrays — efficient manipulation
- Data structures — linked lists, trees
Declare और Initialize
int *ip; // int pointer float *fp; // float pointer int *np = NULL; // NULL pointer — safe initialization int age = 25; int *ptr = &age; // ptr age को point करता है
Uninitialized pointer random address contain करता है। Use करने पर crash! हमेशा NULL या valid address से initialize करें।
& और * Operators
int x = 42; int *p = &x; printf("x ki value = %d\\n", x); // 42 printf("x ka address = %p\\n", &x); // 0x... (hex) printf("p ki value = %p\\n", p); // Same as &x printf("*p se value = %d\\n", *p); // 42 (dereference) *p = 100; // Pointer se x change karo! printf("x ab = %d\\n", x); // x ab 100 hai!
Pointer Arithmetic
int arr[] = {10, 20, 30}; int *p = arr; // पहले element को point करो printf("%d\\n", *p); // 10 p++; // अगले int पर जाओ (+4 bytes) printf("%d\\n", *p); // 20
सारांश
- Pointer किसी variable का memory address store करता है
- Declare:
int *p; &variable— variable का address मिलता है*pointer— address पर stored value मिलती है (dereference)- Use से पहले हमेशा initialize करें (
NULLsafe है) - Array name = first element का pointer
- Functions में pointer pass करके variables modify करते हैं (pass by reference)
Programs लिखें: (1) 5 variables के address और value print करें (2) Pointers से swap (3) Pointer arithmetic से array traverse (4) Function जो pointer के through min और max return करे।