Programmer Coding

Pointers and Function Arguments in C Language

Pointers and Function Arguments?

Pointers are used as function parameters to hold the address of the parameter when calling the function. This is also called searching. When a function is called by a reference, any changes made to the reference also affect the original variable.

Example Swapping two numbers using Pointer

#include <stdio.h>

// Function prototype

void swap(int *a, int *b);

int main() {

int m = 50, n = 100;

printf(“Before Swapping:\n”);

printf(“m = %d\n”, m);

printf(“n = %d\n\n”, n);

// Call swap function to swap values of m and n

swap(&m, &n);

printf(“After Swapping:\n”);

printf(“m = %d\n”, m);

printf(“n = %d\n”, n);

return 0;

}

// Function to swap two integers using pointers

void swap(int *a, int *b) {

int temp = *a;

*a = *b;

*b = temp;

}

Output

Before Swapping:

m = 50

n = 100

After Swapping:

m = 100

n = 50

Functions returning Pointer variables

Functions can also return a pointer to the calling function. In this case, you should be careful because local variables of a function are not located within the function. They are competent only in tasks. So if you return a pointer attached to a local variable, the pointer will point to nothing when the function ends.

#include <stdio.h>

int* larger(int*, int*);

void main()

{

int a = 40;

int b = 115;

int *p;

p = larger(&a, &b);

printf(“%d is larger”,*p);

}

int* larger(int *x, int *y)

{

if(*x > *y)

return x;

else

return y;

}

Output

115 is larger

Safe ways to return a valid Pointer.

  1. Both use parameters of the function. Since the parameters passed to the function are declared in the call, they will be available outside the function.
  2. Alternatively, use local static variables in the function and return them. Since static variables exist until the main() function exits, they are present throughout the program.

Pointer to functions

It is possible to declare a pointer pointing to a function which can then be used as an argument in another function. A pointer to a function is declared as follows,

Syntax

type (*pointer-name)(parameter);

Some Example

int (*sum)();   //legal declaration of pointer to function

int *sum();     //This is not a declaration of pointer to function

A function pointer can point to a specific function when it is assigned the name of that function.

int sum(int, int);

int (*s)(int, int);

s = sum;

Here s is a pointer to a function sum. Now sum can be called using function pointer s along with providing the required argument values.

s (10, 20);

Example of Pointer to Function

#include <stdio.h>

// Function prototype

int sum(int x, int y);

int main() {

// Declare a function pointer fp that points to a function taking two integers and returning an integer

int (*fp)(int, int);

// Assign the address of the sum function to the function pointer fp

fp = sum;

// Call the function through the function pointer and store the result in variable s

int s = fp(55, 80);

// Print the result

printf(“Sum is %d\n”, s);

return 0;

}

// Definition of the sum function

int sum(int x, int y) {

return x + y;

}

Output

Sum is 135

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top