Pointers and functions work together in many important C programming tasks. A pointer can be passed to a function so that the function can access or modify the original variable. Pointers can also be used to pass arrays, strings, structures, and dynamically allocated memory to functions.
Pointers allow a function to work with the original data through its address. This is useful when a function needs to modify a variable.
void change(int *p)
{
*p = 100;
}
The function receives the address of an integer.
When a normal variable is passed to a function, its value is copied.
void change(int x)
{
x = 100;
}
int main()
{
int number = 10;
change(number);
printf("%d", number);
return 0;
}
The original number remains 10.
A pointer can be passed to a function using the address of the variable.
void change(int *p)
{
*p = 100;
}
int main()
{
int number = 10;
change(&number);
printf("%d", number);
return 0;
}
The original variable becomes 100.
A pointer parameter is declared using the * symbol.
void update(int *p)
{
*p = 50;
}
The function can dereference p to access the integer it points to.
When a function expects a pointer to an integer, pass the address of an
integer using &.
int number = 20;
update(&number);
The function receives the address of number.
The function can use *p to access the original value.
void display(int *p)
{
printf("%d", *p);
}
If p points to a variable containing 25, the function
prints 25.
A pointer parameter allows a function to modify the object it points to.
void doubleValue(int *p)
{
*p = *p * 2;
}
If the original value is 10, it becomes 20.
A function can receive more than one pointer.
void update(int *a, int *b)
{
*a = 100;
*b = 200;
}
The function can modify both original variables.
Pointers are commonly used to swap two variables.
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
The function changes the original variables because it receives their addresses.
int x = 10;
int y = 20;
swap(&x, &y);
printf("%d %d", x, y);
After the function call, x contains 20 and
y contains 10.
A function can return one value directly. A pointer parameter can allow a function to modify one or more original objects.
int square(int x)
{
return x * x;
}
void squareInPlace(int *x)
{
*x = *x * *x;
}
When an array is passed to a function, the parameter is adjusted to a pointer to its first element.
void display(int *arr, int size)
{
for(int i = 0; i < size; i++)
{
printf("%d ", arr[i]);
}
}
The size is normally passed separately.
The same array can be processed using pointer arithmetic.
void display(int *ptr, int size)
{
for(int i = 0; i < size; i++)
{
printf("%d ", *(ptr + i));
}
}
int sumArray(int *arr, int size)
{
int sum = 0;
for(int i = 0; i < size; i++)
{
sum += *(arr + i);
}
return sum;
}
The function receives the address of the first array element and the number of elements.
int largest(int *arr, int size)
{
int max = arr[0];
for(int i = 1; i < size; i++)
{
if(arr[i] > max)
{
max = arr[i];
}
}
return max;
}
Strings can be passed to functions using character pointers.
void display(char *text)
{
while(*text != '\0')
{
printf("%c", *text);
text++;
}
}
The pointer moves through the characters until it reaches the null character.
int length(char *text)
{
int count = 0;
while(*text != '\0')
{
count++;
text++;
}
return count;
}
This function counts characters using a pointer.
A pointer to pointer can be passed when a function needs to modify a pointer itself.
void setPointer(int **p, int *value)
{
*p = value;
}
Here, p is a pointer to a pointer.
#include <stdio.h>
void setPointer(int **p, int *value)
{
*p = value;
}
int main()
{
int number = 50;
int *ptr = NULL;
setPointer(&ptr, &number);
printf("%d", *ptr);
return 0;
}
The function changes where ptr points.
A function can receive a pointer to constant data when it should read the data without modifying it through that pointer.
void display(const int *arr, int size)
{
for(int i = 0; i < size; i++)
{
printf("%d ", arr[i]);
}
}
The function can read the array but cannot modify its elements through
arr.
A function can return a pointer, but the pointer must refer to an object that remains valid after the function returns.
int *getValue(int *p)
{
return p;
}
The function above returns the pointer supplied to it.
Do not return the address of an ordinary local variable because its lifetime ends when the function returns.
Pointers are used with dynamic memory allocation functions such as
malloc() and free().
#include <stdlib.h>
int *ptr = malloc(5 * sizeof(int));
if(ptr != NULL)
{
ptr[0] = 10;
}
free(ptr);
The memory returned by malloc() remains allocated until it is
released using free().
#include <stdio.h>
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
int main()
{
int x = 10;
int y = 20;
swap(&x, &y);
printf("x = %d\n", x);
printf("y = %d\n", y);
return 0;
}
void doubleArray(int *arr, int size)
{
for(int i = 0; i < size; i++)
{
arr[i] = arr[i] * 2;
}
}
The function changes the original array elements because it receives a pointer to the first element.
If a function expects an int *, pass the address of an integer or
a compatible pointer.
void change(int *p)
{
*p = 100;
}
int number = 10;
change(&number);
Passing number instead of &number would not match
the expected pointer parameter.
A pointer parameter may be NULL. Do not dereference it without
checking when null is a possible input.
void display(int *p)
{
if(p != NULL)
{
printf("%d", *p);
}
}
#include <stdio.h>
void calculate(int *a, int *b, int *sum, int *product)
{
*sum = *a + *b;
*product = *a * *b;
}
int main()
{
int x = 10;
int y = 5;
int sum;
int product;
calculate(&x, &y, &sum, &product);
printf("Sum = %d\n", sum);
printf("Product = %d\n", product);
return 0;
}
This function uses pointer parameters to store multiple results in variables provided by the caller.
#include <stdio.h>
int sumArray(const int *arr, int size)
{
int sum = 0;
for(int i = 0; i < size; i++)
{
sum += arr[i];
}
return sum;
}
int main()
{
int numbers[] = {10, 20, 30, 40, 50};
int result = sumArray(numbers, 5);
printf("Sum = %d", result);
return 0;
}
The function receives a pointer to constant array data and returns the sum.
Pointers and functions are commonly used together for:
Create a C program that:
main().Then create a second function that receives an integer array through a pointer and finds the largest element.
& when passing the address of a normal variable.* to dereference a pointer inside the function.const pointer parameters when a function should not modify the pointed-to data.Question: Why is a pointer passed to a function when the function needs to modify the original variable?