# 7.3. How do we pass an array to a function?#

To pass an array to a function, all what you need to do is pass the pointer to the first element of the array. For example, in the following code skeleton, we show how do we pass an array to a function.

Code Skeleton

 1#include <stdio.h>
2double f(int []);
3
4int main(void){
5    int x = {1, 7, 3};
6    double result = f(x);
7    return 0;
8}
9double f(int list[]){
10    //statements;
11}


In line $$2$$, the input data type is int []. This means that when the function is called, the input will be a pointer to the first element in the array, which is also the array identifier.

In line $$6$$, we pass to the function f a pointer to the first element of the array, which is also the array identifier: f(x).

In line $$9$$, the function header will receive the pointer of the first element in the array in list. Since x in the main function is the pointer to the first element in the array and is the array identifier, list is also pointing to the first element in the array and is the array identifier in function f.

## 7.3.1. Size of array in a function is unknown!#

Within the function, we can access elements as we see appropriate. However, the only problem is that within the function f, the size of the array list is unknown! If we need to loop over all the elements in the array to calculate the sum of the elements, for example, we will not know when to stop incrementing the index. Hence, we need to pass the size of the array along with the pointer to the first element in the array.

Exercise. Let’s write a program that finds the sum of the elements in an array. The program should call a function to calculate the sum of the elements in an array.

This is similar to finding the average of the elements in an array.

Code

 1#include <stdio.h>
2
3int sumData(int[], const int);
4
5int main(void) {
6  const int size = 3;
7  int x[size] = {1, 7, 3};
8  int result = sumData(x, size);
9  printf("Sum of elements in the array: %d.\n", result);
10  return 0;
11}
12
13int sumData(int list[], const int size) {
14  int sum = 0;
15  for (int index = 0; index < size; index++) {
16    sum = sum + list[index];
17  }
18  return sum;
19}


Output

Sum of elements in the array: 11.


In line $$3$$, we define the inputs to the function as int[], which is the type of the pointer to the first element in an array and int, which is the type of the size of the array.

In line $$13$$, we receive x in list, and size in main as size in sumData.

In line $$16$$, we access the elements of list array as we would do with arrays normally. Important: list is pointing towards what x is pointing to. Both are pointing to the first element in the array.

## 7.3.2. Can I use the pointer syntax too?#

Since array identifiers are also pointers, is it possible to index elements in the array using pointers instead of []? Yes, it is possible, and we show below how.

Code

 1#include <stdio.h>
2
3int sumData(int*, int);
4
5int main(void) {
6  const int size = 3;
7  int x[size] = {1, 7, 3};
8  int result = sumData(x, size);
9  printf("Sum of elements in the array: %d.\n", result);
10  return 0;
11}
12
13int sumData(int* list, int size) {
14  int sum = 0;
15  for (int index = 0; index < size; index++) {
16    sum = sum + *(list + index);
17  }
18  return sum;
19}


Output

Sum of elements in the array: 11.


In line $$3$$, we accept int* instead of int[], because they are equivalent.

In line $$13$$, we accept x into int* list, because they are equivalent. x is a pointer and list is also a pointer.

In line $$16$$, we can access elements in the array by adding i to the pointer list and dereferencing: *(list + i). This is because *(list + i) is equivalent to list[i].

Important!

The syntax of pointers – * and & – and syntax of arrays – [] are interchangeable. This means that we can use any syntax at any time in our program as long as the statements are correct! For example,

Code

 1#include <stdio.h>
2
3// Input is int*
4int sumData(int*, int);
5
6int main(void) {
7  const int size = 3;
8  int x[size] = {1, 7, 3};
9  int result = sumData(x, size);
10  printf("Sum of elements in the array: %d.\n", result);
11  return 0;
12}
13
14// Input is int[]
15int sumData(int list[], const int size) {
16  int sum = 0;
17  for (int index = 0; index < size; index++) {
18    // Index list using pointers
19    sum = sum + *(list + index);
20  }
21  return sum;
22}


Output

Sum of elements in the array: 11.


In line $$4$$, we define the input as a pointer: int*. However, we set the input in the header of the function as int[] in line $$15$$, since int* and int[] are equivalent. Similarly, in line $$19$$, we index in the array list as *(list + index), since it is equivalent to list[i].

## 7.3.3. Are we passing the array by value or by pointers?#

When we pass arrays to functions, we are technically passing a pointer to the first element in the array. To better visualize passing an array to a function, watch the following video.

This means that any changes in the array in the function will be reflected in the main or caller function. For example, let’s write a function that swaps the elements at i and index j in an array. We will write a function named swap that takes in the array as int list[], and the two indices of the elements we want to swap: int i and int j.

We also implement a function that prints the elements of the array. It takes in the array as int list[] and the size of the array as const int size.

In the following code, we print the array x before and after calling the function swap to swap the element at index i = 0 with element at index j = 4.

Code

#include <stdio.h>

void swap(int[], int, int);
void printArray(int[], const int);

int main(void) {
const int size = 5;
int x[size] = {3, 5, 8, 1, 7};
printf("Before swapping: ");
printArray(x, size);
swap(x, 0, 4);
printf("After swapping: ");
printArray(x, size);
return 0;
}

void swap(int list[], int i, int j) {
int temp = list[i];
list[i] = list[j];
list[j] = temp;
}

void printArray(int list[], const int size) {
for (int index = 0; index < size; index++) {
printf("%d ", list[index]);
}
printf("\n");
}


Output

Before swapping: 3 5 8 1 7
After swapping: 7 5 8 1 3


As observed, since we are passing to swap the pointer to the first element in the array, any change to the array in the function is also reflected in the caller function.