🟡 Intermediate  ·  Lesson 26

Pointers & Functions

Pointers और Functions

Pointers with Functions

Pointers allow functions to work directly with the original variables by using their addresses. This is useful when a function needs to modify more than one value.

Call by Value

In call by value, a copy of the value is passed. Changes inside the function do not affect the original variable.

C Language
#include <stdio.h>

void change(int x) {
    x = 100;
}

int main() {
    int a = 10;
    change(a);
    printf("a = %d", a);
    return 0;
}
a = 10

Call by Address

In call by address, the address of the variable is passed. The function uses pointer parameters to modify the original value.

C Language
#include <stdio.h>

void swap(int *x, int *y) {
    int temp = *x;
    *x = *y;
    *y = temp;
}

int main() {
    int a = 10, b = 20;
    swap(&a, &b);
    printf("a = %d, b = %d", a, b);
    return 0;
}
a = 20, b = 10

Function with Array Pointer

C Language
#include <stdio.h>

int sumArray(int *arr, int n) {
    int sum = 0;
    for(int i = 0; i < n; i++)
        sum += arr[i];
    return sum;
}

int main() {
    int a[] = {5, 10, 15, 20};
    printf("Sum = %d", sumArray(a, 4));
    return 0;
}
Sum = 50

Returning Pointer from Function

⚠️ Never Return Local Variable Address

A local variable is destroyed when the function ends. Returning its address creates a dangling pointer.

C Language
#include <stdio.h>

int* greater(int *a, int *b) {
    if(*a > *b)
        return a;
    else
        return b;
}

int main() {
    int x = 40, y = 60;
    int *p = greater(&x, &y);
    printf("Greater = %d", *p);
    return 0;
}
Greater = 60

Summary

  • Call by value sends a copy
  • Call by address sends variable address
  • Use * to access or change original value
  • Use & while passing variable address
  • Pointers help functions return or modify multiple values

Functions में Pointer

Pointers की मदद से function original variables के address पर काम कर सकता है। इससे values को directly modify किया जा सकता है।

Call by Value

Call by value में value की copy जाती है। Function के अंदर change original variable को affect नहीं करता।

C Language
void change(int x) {
    x = 100;
}

Call by Address

Call by address में variable का address pass होता है। Function pointer parameter के द्वारा original value change करता है।

C Language
#include <stdio.h>

void swap(int *x, int *y) {
    int temp = *x;
    *x = *y;
    *y = temp;
}

int main() {
    int a = 10, b = 20;
    swap(&a, &b);
    printf("%d %d", a, b);
    return 0;
}
20 10

Array Pointer Function

C Language
int sumArray(int *arr, int n) {
    int sum = 0;
    for(int i = 0; i < n; i++)
        sum += arr[i];
    return sum;
}

Pointer Return करना

⚠️ सावधानी

Local variable का address return न करें, क्योंकि function खत्म होते ही local variable destroy हो जाता है।

सारांश

  • Call by value में copy pass होती है
  • Call by address में address pass होता है
  • Original value change करने के लिए pointer parameter use करें
  • Variable का address भेजने के लिए & use करें
  • Pointer functions multiple values update करने में useful हैं
← Back to C Tutorial