Pointers and Functions
Introduction to Pointers and Functions in C
Before we delve into pointers and functions, let's briefly understand what they are:
Pointers: A pointer is a variable that stores the memory address of another variable.
Functions: A function is a group of statements that together perform a task. Every C program has at least one function, which is 'main()'.
Now, let's understand how pointers and functions interact in the C language.
Pointers as Function Arguments
In C, we can pass variables to a function in two ways:
Pass by Value: In this method, the value of each of the actual arguments in the calling function is copied into the corresponding formal arguments of the called function.
Pass by Reference: In this method, the address of the actual arguments (or the variables) is copied into the formal arguments. Hence, changes made in the formal arguments affect the actual arguments.
Pointers play a crucial role in 'Pass by Reference'.
Here is a simple example to illustrate this:
#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("Number1 = %d\n", num1);
printf("Number2 = %d\n", num2);
return 0;
}
In the above program, the swap()
function is called with the addresses of num1
and num2
. Inside the function, the values at the addresses are swapped, and the effect is seen in the calling function main()
.
Functions Returning Pointers
In C, a function can also return a pointer. However, the most important thing to remember is the function should not return the address of a local variable defined inside the function. This is because local variables get destroyed after the function finishes execution.
Here is a simple example:
#include <stdio.h>
int* add(int *n1, int *n2) {
static int sum;
sum = *n1 + *n2;
return ∑
}
int main() {
int num1 = 5, num2 = 10;
int *total = add(&num1, &num2);
printf("The sum = %d\n", *total);
return 0;
}
In the above program, the add()
function takes pointers to two integers as arguments and returns a pointer to an integer. The add()
function calculates the sum of the two integers and returns the address of the sum.
Pointers to Functions
In C, we can have pointers to functions that allow functions to be passed as parameters to other functions.
Here is an example:
#include <stdio.h>
void display() {
printf("Hello World\n");
}
int main() {
void (*fun_ptr)() = &display; // Function pointer
(*fun_ptr)(); // Invoking function using function pointer
return 0;
}
In the above program, fun_ptr
is a pointer to function display()
. The function is invoked using the function pointer fun_ptr
.
In conclusion, pointers and functions form an essential part of the C programming language. Pointers provide a powerful and flexible method to manipulate data in functions.
Please remember that pointers should be used carefully. Improper use of pointers can lead to various problems like segmentation faults or can also make your code difficult to read and understand.