Examples of Passing Array to Function in C

Example 1: Checking Size After Passing Array as Parameter

C




// C program to pass the array as a function and check its size
#include <stdio.h>
#include <stdlib.h>
 
// Note that arr[] for fun is just a pointer even if square
// brackets are used. It is same as
// void fun(int *arr) or void fun(int arr[size])
void func(int arr[8])
{
    printf("Size of arr[] in func(): %d bytes",
           sizeof(arr));
}
 
// Drive code
int main()
{
    int arr[8] = { 1, 2, 3, 4, 5, 6, 7, 8 };
 
    printf("Size of arr[] in main(): %dbytes\n",
           sizeof(arr));
 
    func(arr);
 
    return 0;
}


Output

Size of arr[] in main(): 32bytes
Size of arr[] in func(): 8 bytes

As we can see,

  • The size of the arr[] in the main() function (where arr[] is declared) is 32 bytes which is = sizeof(int) * 8 = 4 * 8 = 32 bytes.
  • But in the function where the arr[] is passed as a parameter, the size of arr[] is shown as 8 bytes (which is the size of a pointer in C).

It is due to the fact that the array decays into a pointer after being passed as a parameter. One way to deal with this problem is to pass the size of the array as another parameter to the function. This is demonstrated in the below example.

Example 2: Printing Array from the Function

C




// C program to illustrate the use of sizeof operator in C
#include <stdio.h>
 
// function to print array
void printArray(int arr[], int size)
{
    printf("Array Elements: ");
    for (int i = 0; i < size; i++) {
        printf("%d ", arr[i]);
    }
}
 
// driver code
int main()
{
    int arr[8] = { 12, 4, 5, 3, 7, 8, 11, 45 };
 
    // getting the size of array
    int size = sizeof(arr) / sizeof(arr[0]);
 
    printArray(arr, size);
 
    return 0;
}


Output

Array Elements: 12 4 5 3 7 8 11 45 

This size parameter may not be needed only in the case of ‘\0’ terminated character arrays as size can be determined by checking the end of the string character. The below example demonstrates the following.

Example 3: Printing Array of Characters (String) using Function

C




// C Program to print the string using a function
#include <stdio.h>
 
// function to print the string
void printString(char* str)
{
    printf("Array of Characters: ");
 
    int i = 0;
    while (str[i] != '\0') {
        printf("%c ", str[i]);
        i++;
    }
}
 
// Driver program
int main()
{
    char arr[] = "String";
 
    printString(arr);
 
    return 0;
}


Output

Array of Characters: S t r i n g 

As we can see, we were able to deduce the end of the string (character array) using a NULL Character. Moreover, the %s specifier in printf() function uses the same principle so we can directly use this to print string.

Pass Array to Functions in C

In C, the whole array cannot be passed as an argument to a function. However, you can pass a pointer to an array without an index by specifying the array’s name.

Arrays in C are always passed to the function as pointers pointing to the first element of the array.

Similar Reads

Syntax

In C, we have three ways to pass an array as a parameter to the function. In the function definition, use the following syntax:...

Examples of Passing Array to Function in C

Example 1: Checking Size After Passing Array as Parameter...

Predict the Output of the Below C Programs

...

Frequently Asked Questions (FAQs)

...