Public App

a blog about technology

Arrays as Function Arguments

In C language, you can pass arrays as function arguments. When you pass an array to a function, the array is actually passed by reference, which means that the function receives a pointer to the first element of the array. This allows the function to access and modify the elements of the array.

The syntax for passing an array as a function argument is as follows:

return_type function_name(data_type array_name[], int array_size) {
    // function code
}

Here, return_type is the data type of the value that the function returns, function_name is the name of the function, data_type is the data type of the elements in the array, array_name is the name of the array, and array_size is the size of the array.

For example, if you have a function that calculates the sum of the elements in an array of integers, you can define the function as follows:

int sum_array(int my_array[], int array_size) {
    int sum = 0;
    for (int i = 0; i < array_size; i++) {
        sum += my_array[i];
    }
    return sum;
}

In this example, the sum_array function takes an array of integers named my_array and the size of the array as arguments calculates the sum of the elements in the array and returns the sum as an integer.

You can call this function and pass an array to it as follows:

int my_array[5] = {1, 2, 3, 4, 5};
int array_sum = sum_array(my_array, 5);

In this example, the my_array array is passed to the sum_array function and the function calculates the sum of the elements in the array. The array_sum variable is assigned the value 15, which is the sum of the elements in the my_array array.

You can also pass an array of pointers as a function argument, which is an array that holds memory addresses of other variables. The syntax for passing an array of pointers is as follows:

return_type function_name(data_type *array_name[], int array_size) {
    // function code
}

Here, return_type is the data type of the value that the function returns, function_name is the name of the function, data_type is the data type of the variables that the pointers in the array point to, array_name is the name of the array of pointers, and array_size is the size of the array.

For example, if you have a function that swaps the values of two variables using pointers, you can define the function as follows:

void swap(int *x, int *y) {
    int temp = *x;
    *x = *y;
    *y = temp;
}

In this example, the swap function takes two integer pointers x and y as arguments, swaps the values of the variables pointed to by the pointers, and does not return a value.

You can call this function and pass an array of pointers to it as follows:

int a = 1, b = 2;
int *ptr_array[2] = {&a, &b};
swap(ptr_array[0], ptr_array[1]);

In this example, the ptr_array array holds the memory addresses of the a and b variables, and the swap function is called with the pointers to the a and b variables as arguments. The function swaps the values of the a and b.

Amitesh Kumar

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top