Introduction
Pointers and functions become especially powerful when used together in C. A function can receive the address of a variable through a pointer and then access or modify the original variable. This technique is commonly used for swapping values, returning multiple results, modifying arrays, and working with strings. In this chapter, you will practice passing pointers to functions with simple examples that gradually build your understanding from basic pointer arguments to practical programs. Pointers and Functions in C Practice questions with solutions to help you understand the concepts.
Q1. Pass a Pointer to a Function
Problem Statement
Write a C program that passes the address of a variable to a function and displays its value using a pointer.
C Program
#include <stdio.h>
void display(int *ptr)
{
printf("Value = %d", *ptr);
}
int main()
{
int number = 50;
display(&number);
return 0;
}
Sample Output
Value = 50
Explanation
The function is:
void display(int *ptr)
The parameter ptr is an integer pointer.
In main():
display(&number);
we pass the address of number.
Inside the function:
*ptr
accesses the value stored at that address.
So:
number → 50
↑
|
ptr
The function can therefore access the original variable through its address.
Concepts Covered
- Function
- Pointer parameter
- Address operator
& - Dereferencing
*
Q2. Change a Variable Using a Function and Pointer
Problem Statement
Write a C program to change the value of a variable inside a function using a pointer.
C Program
#include <stdio.h>
void changeValue(int *ptr)
{
*ptr = 100;
}
int main()
{
int number = 20;
printf("Before = %d\n", number);
changeValue(&number);
printf("After = %d", number);
return 0;
}
Sample Output
Before = 20
After = 100
Explanation
Initially:
number = 20
We pass its address:
changeValue(&number);
Inside the function:
*ptr = 100;
changes the value at that address.
Since ptr points to number, the original variable changes.
This is different from passing an ordinary integer value because the function receives access to the original object through its address.
Concepts Covered
- Pointer parameter
- Modifying original variable
- Function arguments
- Dereferencing
Q3. Swap Two Numbers Using a Function and Pointers
Problem Statement
Write a C program to swap two numbers using a function and pointers.
C Program
#include <stdio.h>
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
int main()
{
int first = 10;
int second = 20;
printf("Before swapping:\n");
printf("First = %d\n", first);
printf("Second = %d\n", second);
swap(&first, &second);
printf("\nAfter swapping:\n");
printf("First = %d\n", first);
printf("Second = %d", second);
return 0;
}
Sample Output
Before swapping:
First = 10
Second = 20
After swapping:
First = 20
Second = 10
Explanation
The function receives two addresses:
swap(&first, &second);
Inside the function:
temp = *a;
stores the first value.
Then:
*a = *b;
copies the second value into the first variable.
Finally:
*b = temp;
puts the original first value into the second variable.
Pointers allow the function to modify the original variables.
Concepts Covered
- Multiple pointer parameters
- Swapping
- Function arguments
- Dereferencing
Q4. Find the Larger of Two Numbers Using Pointers
Problem Statement
Write a C program that passes two numbers to a function using pointers and returns the larger number.
C Program
#include <stdio.h>
int findLarger(int *a, int *b)
{
if (*a > *b)
{
return *a;
}
else
{
return *b;
}
}
int main()
{
int first = 45;
int second = 72;
int result = findLarger(&first, &second);
printf("Larger number = %d", result);
return 0;
}
Sample Output
Larger number = 72
Explanation
The function receives:
int *a
int *b
The call is:
findLarger(&first, &second);
Inside the function:
*a
represents the value of first.
And:
*b
represents the value of second.
The function compares these values and returns the larger one.
Concepts Covered
- Pointer parameters
- Function return value
- Comparison
- Dereferencing
Q5. Increment a Number Using a Function
Problem Statement
Write a C program to increase a number by 1 using a function and a pointer.
C Program
#include <stdio.h>
void increment(int *number)
{
(*number)++;
}
int main()
{
int value = 10;
printf("Before = %d\n", value);
increment(&value);
printf("After = %d", value);
return 0;
}
Sample Output
Before = 10
After = 11
Explanation
We pass the address:
increment(&value);
Inside the function:
(*number)++;
increases the value stored at that address.
The parentheses are important:
(*number)++
means:
Increase the value pointed to by
number.
This is different from:
*number++
which is parsed differently because of operator precedence.
Concepts Covered
- Pointer parameters
- Increment operator
- Dereferencing
- Operator precedence
Q6. Return Two Results from a Function Using Pointers in c
Problem Statement
Write a C program to calculate the sum and difference of two numbers using a function and pointer parameters.
C Program
#include <stdio.h>
void calculate(int a, int b, int *sum, int *difference)
{
*sum = a + b;
*difference = a - b;
}
int main()
{
int first = 30;
int second = 10;
int sum;
int difference;
calculate(first, second, &sum, &difference);
printf("Sum = %d\n", sum);
printf("Difference = %d", difference);
return 0;
}
Sample Output
Sum = 40
Difference = 20
Explanation
A normal C function can directly return one value using return.
But sometimes we want a function to produce multiple results.
Here:
calculate(first, second, &sum, &difference);
passes the addresses of sum and difference.
Inside the function:
*sum = a + b;
*difference = a - b;
updates the original variables in main().
This is a very useful pattern when a function needs to produce more than one result.
Concepts Covered
- Pointer output parameters
- Multiple results
- Function arguments
- Dereferencing
Q7. Find the Sum of an Array Using a Function and Pointer
Problem Statement
Write a C program to pass an array to a function using a pointer and calculate the sum of its elements.
C Program
#include <stdio.h>
int calculateSum(int *ptr, int size)
{
int sum = 0;
int i;
for (i = 0; i < size; i++)
{
sum = sum + *(ptr + i);
}
return sum;
}
int main()
{
int numbers[] = {10, 20, 30, 40, 50};
int sum = calculateSum(numbers, 5);
printf("Sum = %d", sum);
return 0;
}
Sample Output
Sum = 150
Explanation
The function parameter is:
int *ptr
The array is passed like this:
calculateSum(numbers, 5);
In this context, numbers provides access to the first element of the array.
Inside the function:
*(ptr + i)
accesses each array element.
For example:
*(ptr + 0) → 10
*(ptr + 1) → 20
*(ptr + 2) → 30
*(ptr + 3) → 40
*(ptr + 4) → 50
The function returns:
150
Concepts Covered
- Arrays as function arguments
- Pointer parameters
- Pointer arithmetic
- Function return value
Q8. Modify All Array Elements Using a Function
Problem Statement
Write a C program to multiply every element of an array by 2 using a function and pointer.
C Program
#include <stdio.h>
void doubleArray(int *ptr, int size)
{
int i;
for (i = 0; i < size; i++)
{
*(ptr + i) = *(ptr + i) * 2;
}
}
int main()
{
int numbers[] = {5, 10, 15, 20, 25};
doubleArray(numbers, 5);
printf("Updated array:\n");
for (int i = 0; i < 5; i++)
{
printf("%d ", numbers[i]);
}
return 0;
}
Sample Output
Updated array:
10 20 30 40 50
Explanation
The array is passed to:
doubleArray(numbers, 5);
Inside the function:
*(ptr + i) = *(ptr + i) * 2;
changes each array element.
For example:
5 → 10
10 → 20
15 → 30
20 → 40
25 → 50
Because the function works with the original array elements, the changes are visible in main().
Concepts Covered
- Array and pointer
- Function parameters
- Pointer arithmetic
- Modifying array elements
Q9. Find the Largest Array Element Using a Function
Problem Statement
Write a C program to find the largest element of an array using a function and pointer.
C Program
#include <stdio.h>
int findLargest(int *ptr, int size)
{
int largest = *ptr;
int i;
for (i = 1; i < size; i++)
{
if (*(ptr + i) > largest)
{
largest = *(ptr + i);
}
}
return largest;
}
int main()
{
int numbers[] = {25, 80, 45, 90, 30};
int largest = findLargest(numbers, 5);
printf("Largest = %d", largest);
return 0;
}
Sample Output
Largest = 90
Explanation
The function receives:
int *ptr
and the number of elements:
int size
The first element becomes the initial largest value:
int largest = *ptr;
Then the function checks every remaining element:
if (*(ptr + i) > largest)
If a larger value is found, largest is updated.
Finally, the function returns 90.
Concepts Covered
- Pointer parameter
- Array traversal
- Pointer arithmetic
- Function return value
Q10. Reverse an Array Using a Function and Two Pointers
Problem Statement
Write a C program to reverse an array using a function with two pointers.
C Program
#include <stdio.h>
void reverseArray(int *start, int *end)
{
int temp;
while (start < end)
{
temp = *start;
*start = *end;
*end = temp;
start++;
end--;
}
}
int main()
{
int numbers[] = {10, 20, 30, 40, 50};
reverseArray(&numbers[0], &numbers[4]);
printf("Reversed array:\n");
for (int i = 0; i < 5; i++)
{
printf("%d ", numbers[i]);
}
return 0;
}
Sample Output
Reversed array:
50 40 30 20 10
Explanation
The function receives two pointers:
int *start
int *end
The call is:
reverseArray(&numbers[0], &numbers[4]);
Initially:
10 20 30 40 50
↑ ↑
start end
The first swap produces:
50 20 30 40 10
Then:
start++;
end--;
moves the pointers toward the center.
The second swap produces:
50 40 30 20 10
The loop stops when the two pointers meet or cross.
This combines functions, pointers, arrays, pointer arithmetic, and swapping in one practical program.
Concepts Covered
- Functions
- Multiple pointer parameters
- Arrays
- Pointer increment/decrement
- Swapping
- Array reversal
Key Takeaways
- Pointers can be passed to functions as arguments.
- Use
&variablewhen passing the address of a variable. - Use
*pointerinside the function to access the pointed-to value. - A function can modify the original variable through a valid pointer.
- C technically uses pass-by-value, including when pointer arguments are passed.
- Pointer parameters are commonly used with arrays.
- A function can use pointer parameters to produce multiple output values.
- Pointers make swapping two variables possible without returning both values.
- Array elements can be modified inside a function through a pointer.
- Two pointer parameters can be used for operations such as array reversal.
- Always make sure a pointer passed to a function points to a valid object before dereferencing it.
- Pointer-based functions are an important foundation for advanced C programming.
FAQs
1. Why are pointers passed to functions in C?
Pointers are commonly passed to functions when the function needs to access or modify the original object or work with an array.
2. How do I pass a variable’s address to a function?
Use the address operator &.
int number = 50;
changeValue(&number);
If the function expects:
void changeValue(int *ptr)
then ptr receives the address of number.
3. Can a function modify the original variable using a pointer?
Yes. For example:
void change(int *ptr)
{
*ptr = 100;
}
Calling:
change(&number);
allows the function to modify number.
4. Is C call by reference or call by value?
C uses pass-by-value. When a pointer is passed, the pointer value—the address—is copied into the function parameter. The function can then use that address to access or modify the original object.
5. How can a function return multiple values in C?
Pointer parameters can be used as output parameters.
void calculate(int a, int b, int *sum, int *difference)
{
*sum = a + b;
*difference = a - b;
}
The function can then write results into multiple variables.
6. How are arrays passed to functions using pointers?
An array can be passed to a function where a parameter receives a pointer to its first element.
void display(int *ptr, int size)
{
for (int i = 0; i < size; i++)
{
printf("%d ", *(ptr + i));
}
}
7. Why are pointers and functions important in C?
Together, pointers and functions allow programs to modify original data, process arrays efficiently, return multiple results, work with strings and structures, and build more advanced programs.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
