Passing pointers to functions in C

Introduction

जब हम functions का उपयोग करते हैं, तो कई बार हमें variables की values function में भेजनी होती हैं।
अब तक हमने दो तरीके देखे:

  • Call by Value → value की copy जाती है
  • Call by Reference → address जाता है

Pointer का use करके हम call by reference achieve करते हैं।

इसका मतलब:

  • हम variable का address function को देते हैं
  • function original variable को modify कर सकता है

यह concept बहुत important है क्योंकि:

  • memory efficient होता है
  • large data structures (arrays, structures) में useful है
  • real programming में extensively use होता है

Definition

Passing Pointers to Functions का मतलब है function को variable का address देना ताकि वह original variable पर operation कर सके।

Basic Concept

Normal call:

change(a);

Pointer call:

change(&a);

Function:

void change(int *x)

Example 1: Value Change करना

#include <stdio.h>

void change(int *x)
{
*x = *x + 10;
}

int main()
{
int a = 5;

change(&a);
printf("%d", a);
return 0;
}

Output:

15

Explanation:

  • address pass हुआ
  • function ने original value modify की

Example 2: Swap Two Numbers

#include <stdio.h>

void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}

int main()
{
int x = 5, y = 10;
swap(&x, &y);
printf("%d %d", x, y);
return 0;
}

Output:

10 5

Explanation:

  • दोनों variables के address pass हुए
  • values swap हो गई

Example 3: Increment Value

#include <stdio.h>

void increment(int *x)
{
(*x)++;
}

int main()
{
int a = 7;

increment(&a);
printf("%d", a);
return 0;
}

Output:

8

Example 4: Multiple Changes

#include <stdio.h>

void modify(int *a, int *b)
{
*a = *a + 5;
*b = *b + 10;
}

int main()
{
int x = 1, y = 2;

modify(&x, &y);
printf("%d %d", x, y);

return 0;
}

Output:

6 12

Important Concept

Pointer Passing = Call by Reference

Difference: Value vs Pointer

Call by ValuePointer Passing
Value की copy जाती हैAddress जाता है
Original change नहीं होताOriginal change होता है
SafeEfficient

Technical Understanding

Pointer passing का use:

  • swapping
  • large data structures
  • dynamic memory
  • performance optimization

Important Points

  • & → address भेजने के लिए
  • * → value access करने के लिए
  • function original data को modify कर सकता है

Exam Points

  • Pointer passing = call by reference
  • Address pass करना जरूरी है
  • * और & बहुत important हैं
  • Swap program exam में बहुत common है
  • Output-based questions आते हैं

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top