Cover Image for Array to Function in C
82 views

Array to Function in C

You can pass an array to a function in several ways. When you pass an array to a function, you are essentially passing a pointer to the first element of the array. Here are some common methods for passing arrays to functions:

  1. Passing the Array as a Pointer:
    You can pass an array to a function by specifying a pointer as a parameter in the function declaration. The function can then operate on the elements of the array through the pointer.
C
 #include <stdio.h>

 // Function that takes an integer array and its size as parameters
 void printArray(int arr[], int size) {
     for (int i = 0; i < size; i++) {
         printf("%d ", arr[i]);
     }
     printf("\n");
 }

 int main() {
     int myArray[] = {1, 2, 3, 4, 5};
     int size = sizeof(myArray) / sizeof(myArray[0]);

     // Pass the array and its size to the function
     printArray(myArray, size);

     return 0;
 }

In this example, the printArray function takes an integer array arr and its size size as parameters and prints the elements of the array.

  1. Passing the Array Length Separately:
    Another common approach is to pass the array length as a separate parameter to the function. This is useful when the array’s size is not known in advance or when working with arrays of different sizes.
C
 #include <stdio.h>

 // Function that takes an integer array and its size as parameters
 void printArray(int arr[], int size) {
     for (int i = 0; i < size; i++) {
         printf("%d ", arr[i]);
     }
     printf("\n");
 }

 int main() {
     int myArray[] = {1, 2, 3, 4, 5};
     int size = sizeof(myArray) / sizeof(myArray[0]);

     // Pass the array and its size to the function
     printArray(myArray, size);

     return 0;
 }

In this example, the printArray function takes an integer array arr and its size size as parameters and prints the elements of the array.

  1. Using Pointers:
    You can also pass arrays to functions using pointers. This is essentially passing a pointer to the first element of the array. The function receives the pointer and can access the array elements through pointer arithmetic.
C
 #include <stdio.h>

 // Function that takes a pointer to an integer and its size
 void printArray(int *arr, int size) {
     for (int i = 0; i < size; i++) {
         printf("%d ", arr[i]);
     }
     printf("\n");
 }

 int main() {
     int myArray[] = {1, 2, 3, 4, 5};
     int size = sizeof(myArray) / sizeof(myArray[0]);

     // Pass a pointer to the array and its size to the function
     printArray(myArray, size);

     return 0;
 }

In this example, the printArray function takes a pointer to an integer arr and its size size as parameters. It uses pointer arithmetic to access the array elements.

All these methods allow you to pass arrays to functions in C. The choice of method depends on your specific requirements and coding style. When you pass an array to a function, you can modify the elements of the original array within the function, as you are working with a pointer to the actual array data.

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS