A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/passing-pointers-to-functions-in-c/ below:

Passing Pointers to Functions in C

Passing Pointers to Functions in C

Last Updated : 23 Jul, 2025

Prerequisites: 

Passing the pointers to the function means the memory location of the variables is passed to the parameters in the function, and then the operations are performed. The function definition accepts these addresses using pointers, addresses are stored using pointers.

Arguments Passing without pointer 

When we pass arguments without pointers the changes made by the function would be done to the local variables of the function.

Below is the C program to pass arguments to function without a pointer:

C
// C program to swap two values
//  without passing pointer to 
// swap function.
#include <stdio.h>

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

// Driver code
int main() 
{
  int a = 10, b = 20;
  swap(a, b);
  printf("Values after swap function are: %d, %d",
          a, b);
  return 0;
}

Output
Values after swap function are: 10, 20
Arguments Passing with pointers 

A pointer to a function is passed in this example. As an argument, a pointer is passed instead of a variable and its address is passed instead of its value. As a result, any change made by the function using the pointer is permanently stored at the address of the passed variable. In C, this is referred to as call by reference.

Below is the C program to pass arguments to function with pointers:

C
// C program to swap two values
// without passing pointer to 
// swap function.
#include <stdio.h>

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

// Driver code
int main()
{
  int a = 10, b = 20;
  printf("Values before swap function are: %d, %d\n", 
          a, b);
  swap(&a, &b);
  printf("Values after swap function are: %d, %d", 
          a, b);
  return 0;
}

Output
Values before swap function are: 10, 20
Values after swap function are: 20, 10


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4