Lesson 47 of 60 – Pointer and Function
78%

Pointer and Function in C

Pointers and functions work together in many important C programming tasks. A pointer can be passed to a function so that the function can access or modify the original variable. Pointers can also be used to pass arrays, strings, structures, and dynamically allocated memory to functions.

Note: C passes function arguments by value. When a pointer is passed to a function, the pointer itself is copied, but the copied pointer can still be used to access and modify the original object it points to.

1. Why Use Pointers with Functions?

Pointers allow a function to work with the original data through its address. This is useful when a function needs to modify a variable.

void change(int *p)
{
    *p = 100;
}

The function receives the address of an integer.

2. Passing a Normal Variable

When a normal variable is passed to a function, its value is copied.

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

int main()
{
    int number = 10;

    change(number);

    printf("%d", number);

    return 0;
}

The original number remains 10.

3. Passing a Pointer

A pointer can be passed to a function using the address of the variable.

void change(int *p)
{
    *p = 100;
}

int main()
{
    int number = 10;

    change(&number);

    printf("%d", number);

    return 0;
}

The original variable becomes 100.

4. Function Parameter as Pointer

A pointer parameter is declared using the * symbol.

void update(int *p)
{
    *p = 50;
}

The function can dereference p to access the integer it points to.

5. Address Operator in Function Call

When a function expects a pointer to an integer, pass the address of an integer using &.

int number = 20;

update(&number);

The function receives the address of number.

6. Dereferencing Inside a Function

The function can use *p to access the original value.

void display(int *p)
{
    printf("%d", *p);
}

If p points to a variable containing 25, the function prints 25.

7. Modifying a Variable

A pointer parameter allows a function to modify the object it points to.

void doubleValue(int *p)
{
    *p = *p * 2;
}

If the original value is 10, it becomes 20.

8. Function with Two Pointer Parameters

A function can receive more than one pointer.

void update(int *a, int *b)
{
    *a = 100;
    *b = 200;
}

The function can modify both original variables.

9. Swapping Two Numbers

Pointers are commonly used to swap two variables.

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

The function changes the original variables because it receives their addresses.

10. Calling the Swap Function

int x = 10;
int y = 20;

swap(&x, &y);

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

After the function call, x contains 20 and y contains 10.

11. Returning a Value vs Modifying Through a Pointer

A function can return one value directly. A pointer parameter can allow a function to modify one or more original objects.

int square(int x)
{
    return x * x;
}

void squareInPlace(int *x)
{
    *x = *x * *x;
}

12. Passing an Array to a Function

When an array is passed to a function, the parameter is adjusted to a pointer to its first element.

void display(int *arr, int size)
{
    for(int i = 0; i < size; i++)
    {
        printf("%d ", arr[i]);
    }
}

The size is normally passed separately.

13. Passing an Array with Pointer Arithmetic

The same array can be processed using pointer arithmetic.

void display(int *ptr, int size)
{
    for(int i = 0; i < size; i++)
    {
        printf("%d ", *(ptr + i));
    }
}

14. Finding Sum Using a Pointer Function

int sumArray(int *arr, int size)
{
    int sum = 0;

    for(int i = 0; i < size; i++)
    {
        sum += *(arr + i);
    }

    return sum;
}

The function receives the address of the first array element and the number of elements.

15. Finding the Largest Value

int largest(int *arr, int size)
{
    int max = arr[0];

    for(int i = 1; i < size; i++)
    {
        if(arr[i] > max)
        {
            max = arr[i];
        }
    }

    return max;
}

16. Pointer and String Function

Strings can be passed to functions using character pointers.

void display(char *text)
{
    while(*text != '\0')
    {
        printf("%c", *text);
        text++;
    }
}

The pointer moves through the characters until it reaches the null character.

17. Counting Characters with a Pointer

int length(char *text)
{
    int count = 0;

    while(*text != '\0')
    {
        count++;
        text++;
    }

    return count;
}

This function counts characters using a pointer.

18. Pointer to Pointer as a Function Parameter

A pointer to pointer can be passed when a function needs to modify a pointer itself.

void setPointer(int **p, int *value)
{
    *p = value;
}

Here, p is a pointer to a pointer.

19. Example of Pointer to Pointer

#include <stdio.h>

void setPointer(int **p, int *value)
{
    *p = value;
}

int main()
{
    int number = 50;
    int *ptr = NULL;

    setPointer(&ptr, &number);

    printf("%d", *ptr);

    return 0;
}

The function changes where ptr points.

20. Pointer Parameter with const

A function can receive a pointer to constant data when it should read the data without modifying it through that pointer.

void display(const int *arr, int size)
{
    for(int i = 0; i < size; i++)
    {
        printf("%d ", arr[i]);
    }
}

The function can read the array but cannot modify its elements through arr.

21. Returning a Pointer from a Function

A function can return a pointer, but the pointer must refer to an object that remains valid after the function returns.

int *getValue(int *p)
{
    return p;
}

The function above returns the pointer supplied to it.

Do not return the address of an ordinary local variable because its lifetime ends when the function returns.

22. Pointer and Dynamic Memory

Pointers are used with dynamic memory allocation functions such as malloc() and free().

#include <stdlib.h>

int *ptr = malloc(5 * sizeof(int));

if(ptr != NULL)
{
    ptr[0] = 10;
}

free(ptr);

The memory returned by malloc() remains allocated until it is released using free().

23. Pointer Function for Swapping

#include <stdio.h>

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

    *a = *b;
    *b = temp;
}

int main()
{
    int x = 10;
    int y = 20;

    swap(&x, &y);

    printf("x = %d\n", x);
    printf("y = %d\n", y);

    return 0;
}

24. Pointer Function for Array Update

void doubleArray(int *arr, int size)
{
    for(int i = 0; i < size; i++)
    {
        arr[i] = arr[i] * 2;
    }
}

The function changes the original array elements because it receives a pointer to the first element.

25. Common Mistake: Passing the Wrong Argument

If a function expects an int *, pass the address of an integer or a compatible pointer.

void change(int *p)
{
    *p = 100;
}

int number = 10;

change(&number);

Passing number instead of &number would not match the expected pointer parameter.

26. Common Mistake: Dereferencing NULL

A pointer parameter may be NULL. Do not dereference it without checking when null is a possible input.

void display(int *p)
{
    if(p != NULL)
    {
        printf("%d", *p);
    }
}

27. Complete Pointer and Function Program

#include <stdio.h>

void calculate(int *a, int *b, int *sum, int *product)
{
    *sum = *a + *b;
    *product = *a * *b;
}

int main()
{
    int x = 10;
    int y = 5;

    int sum;
    int product;

    calculate(&x, &y, &sum, &product);

    printf("Sum = %d\n", sum);
    printf("Product = %d\n", product);

    return 0;
}

This function uses pointer parameters to store multiple results in variables provided by the caller.

28. Complete Array and Pointer Function Program

#include <stdio.h>

int sumArray(const int *arr, int size)
{
    int sum = 0;

    for(int i = 0; i < size; i++)
    {
        sum += arr[i];
    }

    return sum;
}

int main()
{
    int numbers[] = {10, 20, 30, 40, 50};

    int result = sumArray(numbers, 5);

    printf("Sum = %d", result);

    return 0;
}

The function receives a pointer to constant array data and returns the sum.

29. Practical Uses of Pointer and Function

Pointers and functions are commonly used together for:

  • Swapping values
  • Updating variables
  • Processing arrays
  • Processing strings
  • Returning multiple results through output parameters
  • Dynamic memory management
  • Working with structures
  • Building data structures

30. Practice Program

Create a C program that:

  1. Creates two integer variables.
  2. Creates a function using pointer parameters.
  3. Swaps the two values.
  4. Calculates their sum.
  5. Calculates their product.
  6. Displays all results from main().

Then create a second function that receives an integer array through a pointer and finds the largest element.

📌 Key Points

  • C passes function arguments by value.
  • A pointer parameter allows a function to access the original object through its address.
  • Use & when passing the address of a normal variable.
  • Use * to dereference a pointer inside the function.
  • Pointers are useful for modifying variables inside functions.
  • Arrays passed to functions are accessed through pointer-like parameters.
  • Pass the array size separately when required.
  • Character pointers can be used to process strings.
  • Pointer-to-pointer parameters can modify a pointer itself.
  • Use const pointer parameters when a function should not modify the pointed-to data.
  • Do not dereference NULL or invalid pointers.
  • Do not return the address of a local variable whose lifetime has ended.

🧠 Quick Quiz

Question: Why is a pointer passed to a function when the function needs to modify the original variable?