Pointers with Functions in C

Hello there, fellow coder! Today, we’re going to dive into the world of pointers and functions in C. This tutorial is designed to help you understand how to use pointers with functions effectively, and by the end of it, you’ll be a pro at handling pointers in C. So, let’s get started!

Understanding Pointers and Functions

First things first, let’s understand what pointers and functions are. A pointer in C is a variable that stores the address of another variable. On the other hand, a function is a group of statements that together perform a task. Now, when we talk about using pointers with functions, we’re essentially talking about passing the address of a variable to a function or returning the address of a variable from a function.

Declaring a Function Pointer

A function pointer in C is a variable that stores the address of a function that can later be called through that function pointer. The syntax for declaring a function pointer is as follows:

return_type (*pointer_name)(parameter list);

For example, if we have a function foo that takes an integer as an argument and returns void, the declaration of a function pointer for foo would be:

void (*foo_ptr)(int);

Passing Pointers to Functions

In C programming, we can pass a pointer to a function. This technique is known as call by reference. When we pass a pointer to a function, the address of the variable is passed instead of the value. Any change made by the function using the pointer is permanently made at the address of the passed variable.

Here’s an example of passing pointers to a function:

#include <stdio.h>

void addOne(int* ptr) {
    (*ptr)++; // adding 1 to *ptr
}

int main() {
    int* p, i = 10;
    p = &i;
    addOne(p);
    printf("%d", *p); // Output: 11
    return 0;
}
C

In the above example, the address of i is passed to the addOne function. Inside the function, the value at the address is incremented by 1. Hence, the value of i in the main function is also incremented.

Function Pointers as Function Arguments

We can also pass function pointers to other functions as arguments. This is often used in situations where we need to provide some behavior to a function, but we want to allow the exact behavior to be customizable. Here’s an example:

#include <stdio.h>

void printNum(int num) {
    printf("%d\n", num);
}

void operateOnNum(int num, void (*operation)(int)) {
    operation(num);
}

int main() {
    operateOnNum(5, printNum); // Output: 5
    return 0;
}
C

In this example, the operateOnNum function takes an integer and a function pointer as arguments. It then calls the function that the pointer points to, passing the integer as an argument.

Code Examples

Let’s look at two complete C code examples that use pointers with functions and understand their outputs.

Example 1: Swapping Two Numbers

#include <stdio.h>

void swap(int* n1, int* n2) {
    int temp;
    temp = *n1;
    *n1 = *n2;
    *n2 = temp;
}

int main() {
    int num1 = 5, num2 = 10;
    swap(&num1, &num2);
    printf("num1 = %d\n", num1

); // Output: num1 = 10
    printf("num2 = %d", num2); // Output: num2 = 5
    return 0;
}
C

In this example, the addresses of num1 and num2 are passed to the swap function. Inside the function, the values at the addresses are swapped. Hence, the values of num1 and num2 in the main function are also swapped.

Example 2: Modifying Salary

#include <stdio.h>

void salaryHike(int* salary, int bonus) {
    *salary += bonus;
}

int main() {
    int salary = 10000, bonus = 2000;
    salaryHike(&salary, bonus);
    printf("Final salary: %d", salary); // Output: Final salary: 12000
    return 0;
}
C

In this example, the address of salary is passed to the salaryHike function. Inside the function, the bonus is added to the salary. Hence, the value of salary in the main function is also increased.

Wrapping Up

Pointers with functions in C can seem a bit daunting at first, but with practice, they become a powerful tool in your programming arsenal. They allow for more efficient code and can provide a lot of flexibility in how your functions operate.

Frequently Asked Questions (FAQ)

  • How to use pointers with functions in C?

    You can use pointers with functions in C by passing the address of a variable to a function or returning the address of a variable from a function.

  • Can we have a pointer to a function in C?

    Yes, in C, we can have a pointer to a function. This is known as a function pointer.

  • What is a function pointer with an example?

    A function pointer is a variable that stores the address of a function. For example, void (*foo_ptr)(int); is a function pointer that can point to a function that takes an integer as an argument and returns void.

  • How to add two pointers using functions in C?

    You cannot add two pointers directly in C. However, you can add or subtract an integer value to/from a pointer.

  • What is the use of pointers in functions?

    Pointers in functions are used for dynamic memory allocation, for working with arrays and strings, and for achieving pass by reference functionality.

  • What is the difference between passing by value and passing by reference?

    When we pass a variable to a function by value, a new copy of the variable is created which is used inside the function. When we pass a variable by reference, the address of the variable is passed, and the same variable is used inside the function.

  • Can a function return a pointer?

    Yes, a function can return a pointer in C. However, you should be careful not to return a pointer to a local variable as it goes out of scope after the function ends.

  • What is a null pointer in C?

    A null pointer in C is a pointer that does not point to any memory location. It is used when we want to indicate that the pointer is not intended to point to an accessible memory location.

  • What is a dangling pointer in C?

    A dangling pointer in C is a pointer that points to a memory location that has been freed or deleted.

  • What is a far pointer in C?

    A far pointer in C is a pointer that can access all the 16 segments of RAM

  1. Understanding Pointers in C
  2. Pointer Arithmetic in C
  3. Pointers with Arrays in C
  4. Pointers with Strings in C
  5. C Dynamic Memory Allocation

That’s all for this tutorial. I hope you found it helpful and informative. Keep practicing and happy coding!

Scroll to Top