In this section, we will learn how to use arrays in C functions.
Passing Array to Function in C
We can pass an array as the argument to a function as well. But you should know that when we call another function and assign the name of an array as the argument of the function, we’re actually passing the memory-address of that array to another function.
This means if the called function changed the value of any element in the array, the change is actually reflected in the main array as well.
Example: passing array to function in C
#include <stdio.h> void change(int * array , int size); int main() { int array[] = {1,2,3,4,5,6}; int size = sizeof(array)/ sizeof(int); change(array ,size ); printf("The value of the first element in the array is: %d\n",array[0]); return 0; } void change(int * array, int size ){ for (int i = 0 ; i<size; i++){ *(array + i) = i+1000; } }
Output:
The value of the first element in the array is: 1000
In this example, we have an array named `array` and a function named `change` that takes 2 arguments. The first argument is the name of an array which is actually the pointer to the first element of that array and the second argument is the size of that array.
Note: we need to pass the number of elements in the target array to another function as well because there’s no way in figuring out this number just by looking into the pointer of that array alone.
So here, because we’re passing the memory-address of the array to the `change` function, any modification within the body of this function will be reflected in the array inside the `main` function as well.
As you can see inside, the `change` function we modified the elements of the `array` and so when we called the `printf` function in the `main` function to get the value stored in the first element of the array, the actual value is changed. (The original value was 0 but after calling the `change` function, the value now is 1000).
Returning Array from Function in C
Arrays can be returned from functions as well.
Here you should be aware that because the variable that holds an array is basically just a pointer that contains the address of the first element of the array, then the return value of such function would not be a copy of the entire elements of the target array but just a pointer to that array.
Note: C does not advocate returning a local array as the return value of a function! So we need to use the static keyword in order to create such a variable. Check the C static keyword section if you’re not familiar with this keyword.
Example: returning array from function in C
#include <stdio.h> int* returnArray(void); int main() { int * array = returnArray(); printf("The value of the i is: %d\n",array[0]); return 0; } int* returnArray(void){ static int array[] = {1,2,3,4,5,6}; return array; }
Output:
The value of the i is: 1
Constant Array Parameter in C Function
If we want to stop the called a function from modifying the content of an array, we can turn the parameter of the called function into a constant pointer. That way, the called function cannot change the content of the array anymore.
Example: constant array parameter in C function
#include <stdio.h> void change(const int * array , int size); int main() { int array[] = {1,2,3,4,5,6}; int size = sizeof(array)/ sizeof(int); change(array ,size ); printf("The value of the i is: %d\n",array[0]); return 0; } void change(const int * array, int size ){ for (int i = 0 ; i<size; i++){ *(array + i) = i+1000; } }
Output:
error: assignment of read-only location '*(array + (sizetype)((long long unsigned int)i * 4))' *(array + i) = i+1000;
As you can see, the compiler returned error because we tried to change the value of the array in `*(array + i) = i+1000;` expression.