Apply for Zend Framework Certification Training

c



< Pointer Arithmetic in C Dynamic memory allocation >



Passing pointers to functions and returning pointers from functions, Function Pointers:
Pointers that point to functions. with advanced notes with examples
 
1. Passing Pointers to Functions
Why use pointers in functions?
To modify original variables (call by reference)
To avoid copying large data
To work with arrays, structures efficiently
Example 1: Swap using pointers
#include <stdio.h>
void swap(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}
int main() {
    int x = 10, y = 20;
    swap(&x, &y);
    printf("x=%d y=%d", x, y);
}
Key idea:
 
*a means value at address
Changes reflect in main()
Example 2: Modify array using pointer
#include<stdio.h>
int increment(int *pointerArray,int size){
    for(int i=0;i<size;i++){
        *(pointerArray+i)=*(pointerArray+i)+1;
    }
}
int main(){
    int myarray[]={11,22,33,44,55};
    for(int i=0;i<5;i++){
        printf("%d ",myarray[i]);
    }
    int size = 5;
    increment(&myarray[0],size);
    for(int i=0;i<5;i++){
        printf("%d ",myarray[i]);
    }
}
 
Example 3 (Advanced): Pointer to pointer
void update(int **ptr) {
    **ptr = 100;
}
int main() {
    int x = 10;
    int *p = &x;
    update(&p);
    printf("%d", x);
}
Used when:
You want to modify pointer itself
Used in dynamic memory & linked list
2. Returning Pointers from Functions
Important Rule
Never return address of local variable
Wrong:
int* func() {
    int x = 10;
    return &x;  // ? dangerous (dangling pointer)
}
Correct Ways
Method 1: Using static variable
int* func() {
    static int x = 10;
    return &x;
}
Static variable persists after function ends
Method 2: Using malloc (Best practice)
#include <stdlib.h>
int* createArray(int n) {
    int *arr = (int*)malloc(n * sizeof(int));
    return arr;
}
Used in:
Dynamic arrays
Linked lists
Trees
Method 3: Return pointer passed as argument
int* modify(int *p) {
    *p = 50;
    return p;
}
Interview Trap
char* getString() {
    char str[] = "Hello";
    return str; // ? wrong
}
Fix:
char* getString() {
    static char str[] = "Hello";
    return str;
}
3. Function Pointers (Very Important)
Function pointer = pointer that stores address of function
Syntax
return_type (*pointer_name)(parameters);
Example 1: Basic function pointer
#include <stdio.h>
int add(int a, int b) {
    return a + b;
}
int main() {
    int (*fp)(int, int);
    fp = add;
    printf("%d", fp(2, 3));  // 5
}
Example 2: Passing function as argument
int operation(int a, int b, int (*func)(int, int)) {
    return func(a, b);
}
int add(int a, int b) { return a + b; }
int mul(int a, int b) { return a * b; }
int main() {
    printf("%d\n", operation(2, 3, add));
    printf("%d", operation(2, 3, mul));
}
This is like callback function
Example 3: Array of function pointers
#include <stdio.h>
int add(int a, int b) { return a + b; }
int sub(int a, int b) { return a - b; }
int main() {
    int (*arr[2])(int, int) = {add, sub};
    printf("%d\n", arr[0](5, 2)); // add
    printf("%d", arr[1](5, 2));   // sub
}
Example 4 (Advanced): Returning function pointer
int add(int a, int b) { return a + b; }
int (*getFunc())(int, int) {
    return add;
}
int main() {
    int (*fp)(int, int) = getFunc();
    printf("%d", fp(3, 4));
}
Real-World Use Cases
1. Callback functions
Used in:
Sorting (qsort)
Event handling
Interrupts in embedded systems
2. Menu-driven programs
switch(choice) {
    case 1: fp = add; break;
    case 2: fp = sub; break;
}
3. Dynamic behavior (Strategy pattern)
Change logic at runtime
Important Interview Points
 
? Pointer vs array difference
? Dangling pointer vs NULL pointer
? Function pointer syntax (very common question)
? Returning pointer safety
? Double pointer concept
 
 Quick Summary
Concept Key Idea
Passing pointer Modify original variable
Returning pointer Use static/malloc
Function pointer Store function address
Pointer to pointer Modify pointer itself

< Pointer Arithmetic in C Dynamic memory allocation >



Ask a question



  • Question:
    {{questionlistdata.blog_question_description}}
    • Answer:
      {{answer.blog_answer_description  }}
    Replay to Question


Back to Top