Passing Array to Function in C
In C, there are various general problems which requires passing more than one variable of the same type to a function. For example, consider a function which sorts the 10 elements in ascending order. Such a function requires 10 numbers to be passed as the actual parameters from the main function. Here, instead of declaring 10 different numbers and then passing into the function, we can declare and initialize an array and pass that into the function. This will resolve all the complexity since the function will now work for any number of values.
Syntax to pass an array to the function:
cvoid functionName(dataType arrayName[], int arraySize) {
// Function code that uses the array
}
Here are three common methods:
Using a fixed-size array as a parameter:
In this method, you specify the array parameter as int arr[] where int is the data type of the elements in the array. The size parameter is optional and is used to specify the size of the array.cvoid functionWithArray(int arr[], int size) { // Function code that uses the array }Using a pointer to the array:
In this method, you specify the array parameter as int *arr, where int is the data type of the elements in the array. You can also use the size parameter to specify the size of the array. When using a pointer, you can pass an array or a pointer to an array, and the function will work with both.cvoid functionWithArray(int *arr, int size) { // Function code that uses the array }Using a fixed-size array with a defined size:
In this method, you specify the array parameter as int arr[5], where int is the data type of the elements, and 5 is the fixed size of the array. The size is not a true constraint, but it serves as a hint to the programmer about the expected size.cvoid functionWithArray(int arr[5]) { // Function code that uses the array }
Here's an example demonstrating the first two methods:
c#include <stdio.h>
void method1(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
void method2(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]);
method1(myArray, arraySize);
method2(myArray, arraySize);
return 0;
}
Both method1 and method2 receive an array as an argument, and they work with the same array, myArray, using different parameter declarations.C function to sort the array
Example:
c#include <stdio.h>
#include <stdlib.h> // Include the library for qsort
// Comparison function for qsort
int compare(const void *a, const void *b) {
return (*(int*)a - *(int*)b);
}
int main() {
int myArray[] = {64, 34, 25, 12, 22, 11, 90};
int arraySize = sizeof(myArray) / sizeof(myArray[0]);
printf("Original array: ");
for (int i = 0; i < arraySize; i++) {
printf("%d ", myArray[i]);
}
printf("\n");
// Sort the array using qsort
qsort(myArray, arraySize, sizeof(int), compare);
printf("Sorted array: ");
for (int i = 0; i < arraySize; i++) {
printf("%d ", myArray[i]);
}
printf("\n");
return 0;
}
Output:
cOriginal array: 64 34 25 12 22 11 90
Sorted array: 11 12 22 25 34 64 90
Returning array from the function
As we know that, a function can not return more than one value. However, if we try to write the return statement as return a, b, c; to return three values (a,b,c), the function will return the last mentioned value which is c in our case. In some problems, we may need to return multiple values from a function. In such cases, an array is returned from the function.
In C, you cannot return an entire array directly from a function, but you can return a pointer to an array or dynamically allocate an array within a function and return a pointer to that dynamically allocated array.
Example:
c#include <stdio.h>
#include <stdlib.h>
// Function to create and return an array dynamically
int* createAndReturnArray(int size) {
int *arr = (int*)malloc(size * sizeof(int)); // Dynamically allocate memory
if (arr == NULL) {
printf("Memory allocation failed.\n");
exit(1); // Exit the program
}
for (int i = 0; i < size; i++) {
arr[i] = i * 2;
}
return arr; // Return the pointer to the dynamically allocated array
}
int main() {
int arraySize = 5;
int *resultArray = createAndReturnArray(arraySize);
printf("Returned array: ");
for (int i = 0; i < arraySize; i++) {
printf("%d ", resultArray[i]);
}
printf("\n");
free(resultArray); // Don't forget to free the dynamically allocated memory
return 0;
}
Output:
cReturned array: 0 2 4 6 8
In this example, the createAndReturnArray function dynamically allocates an array of integers of the specified size, fills it with values, and returns a pointer to the dynamically allocated array. The main function then uses this returned pointer to access and print the elements of the dynamically created array. Finally, it is essential to free the dynamically allocated memory using the free function to prevent memory leaks.
0 Comments