C Programming Tutorial

C Programming Tutorial

Passing Array to Function in C

Hello Everyone, In this article, we will cover the concept of Passing Array to Function in C Programming. This fundamental concept helps manage multiple data values efficiently in your code. We will explain the process with clear examples, making it easy to understand how arrays and functions work together in C. Whether you’re new to C or have some experience, this guide will help you to understand Basics of Passing Array to Function in C effectively. Let’s get started.

In C programming, when you pass an array to a function, you are actually passing a pointer to the first element of the array. This is because arrays in C are essentially pointers to the memory location where the array elements are stored. As a result, any modifications made to the array within the function will affect the original array.

To pass an array to a function in C, you need to:

  1. Declare the function with the correct parameter type: Specify the data type and use an asterisk for pointer notation, or square brackets to indicate an array parameter.
  2. Call the function with the array as an argument: Pass the array as an argument by providing the array’s name without square brackets.

Example:

#include <stdio.h>
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 arraySize = sizeof(myArray) / sizeof(myArray[0]);

    printArray(myArray, arraySize);

    return 0;
}

In this example, we have a function called printArray that takes two parameters: a pointer to an integer int *arr, which is equivalent to an integer array int arr[], and an integer size. The function iterates through the array and prints its elements using the printf function. In the main function, we declare and initialize an array called myArray and calculate its size. We then call the printArray function, passing myArray and arraySize as arguments.

Ways to Pass arrays as function arguments in C

1. Pass the array as a pointer:

    You can pass an array as a pointer to its first element. This is the most common method, as arrays are implicitly converted to pointers when passed as function arguments.

    Example:

    #include <stdio.h>
    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 arraySize = sizeof(myArray) / sizeof(myArray[0]);
    
        printArray(myArray, arraySize);
    
        return 0;
    }
    

    2. Pass the array using square bracket notation:

      In this method, you declare the function parameter with square brackets to indicate that it’s an array. This is essentially the same as passing the array as a pointer, as the compiler treats the two methods the same way.

      Example:

      #include <stdio.h>
      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 arraySize = sizeof(myArray) / sizeof(myArray[0]);
      
          printArray(myArray, arraySize);
      
          return 0;
      }
      

      3. Pass a pointer to the whole array:

        In this method, you pass a pointer to the entire array by wrapping the array in a struct or using an array of pointers. This is less common, but it allows you to pass arrays of different sizes to the same function.

        Example with a struct:

        #include <stdio.h>
        typedef struct {
            int arr[5];
        } ArrayWrapper;
        
        void printArray(ArrayWrapper *arrayWrapper, int size) {
            for (int i = 0; i < size; i++) {
                printf("%d ", arrayWrapper->arr[i]);
            }
            printf("\\n");
        }
        
        int main() {
            ArrayWrapper myArrayWrapper = { {1, 2, 3, 4, 5} };
            int arraySize = sizeof(myArrayWrapper.arr) / sizeof(myArrayWrapper.arr[0]);
        
            printArray(&myArrayWrapper, arraySize);
        
            return 0;
        }
        

        In all these methods, remember that you are passing the memory address of the first element of the array or a reference to the entire array, not a copy of the array itself. Any changes made to the array within the function will affect the original array.

        Syntax for Pass arrays as function arguments in C

        1. Pass the array as a pointer:

          Function declaration:

          void functionName(int *arr, int size);

          Function call:

          functionName(myArray, arraySize);

          2. Pass the array using square bracket notation:

            Function declaration:

            void functionName(int arr[], int size);

            Function call:

            functionName(myArray, arraySize);

            3. Pass a pointer to the whole array using a struct:

              Struct definition:

              typedef struct {
                  int arr[size]; // Replace 'size' with the desired size of the array
              } ArrayWrapper;
              

              Function declaration:

              void functionName(ArrayWrapper *arrayWrapper, int size);

              Function call:

              functionName(&myArrayWrapper, arraySize);

              Replace functionName with the desired name for your function, and use the appropriate data type and array size as needed. Remember that the first two methods are equivalent and more common, while the third method is less common but allows you to pass arrays of different sizes to the same function.

              Share This Article