In C programming, there are various problems which requires passing more than one variable of the same type to a function. For example, consider a function in which we have to pass the marks of 70 students. Such a function requires 70 numbers to be passed as the actual parameters from the main function. Here, instead of declaring 70 different numbers and then passing them as an argument into the function, we can declare and initialize an array and pass that into the function. This will resolve all the complexity and make the code easy to read.
We can pass the one dimentional and multidimensional array in function as an argument. There are multiple ways to pass one-dimensional or two-dimensional arrays as arguments in function. We pass the array to a function to make it accessible within the function. When we pass an entire array to a function, then the function can access all the elemnts of the array. Single array elements can also be passed as arguments, it could be a sized or unsized array. This can be done in the same way as we pass variables to a function. Following are the syntax of passing array as an argument.
int func(int arr[])
{
int sum;
for (int i = 0; i < 3; i++)
{
sum = sum + arr[i];
}
return sum / 3;
}
// Inside func, if you change the value of element in the array, it gets reflected in the main function too.
// because array doesn't gets copied, the parameter becomes pointers.
int main()
{
int arr[] = {1, 2, 3};
int a = func(arr);
return 0;
}
When we pass the address of an array while calling a function then we are using call by reference function call. When we pass an address as an argument in the function, the pointer in the function receives the address of the array.
void display(int *ptr)
{
printf("%d", *ptr);
}
int main()
{
int arr[] = {1, 2, 3, 4};
for (int i = 0; i < 4; i++)
{
display(&arr[i]);
}
return 0;
Int in this architecture taking only 2 byte
*arr = value at arr(1000)
*arr+1 = value at arr(1002)
and so on
#include <stdio.h>
int func1(int array[])
{
for (int i = 0; i < 4; i++)
{
printf("The value at index %d is %d\n", i, array[i]);
}
array[0] = 33;
return 0;
}
void func2(int *ptr)
{
for (int i = 0; i < 4; i++)
{
// printf("The value at %d is %d\n",i,ptr[i]); we can also use the code below to access elements
printf("The value at %d is %d\n", i, *(ptr + i));
}
*(ptr + 2) = 653;
}
void func3(int arr[2][2])
{
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
printf("Value at d2arr[%d][%d]is : %d\n", i, j, arr[i][j]);
}
}
}
int main()
{
int arr[] = {23, 13, 3, 4};
int d2arr[2][2] = {{1, 2}, {3, 4}};
// printf("The value at index 0 is %d\n", arr[0]);
// func1(arr); //here we are going to change the value of arr[i] inside the func. and it will get reflected to the main array also.
// printf("The value at index 0 is %d\n", arr[0]);
// func2(arr); // first time it prints all the element then after the loop it updates the element and then
// func2(arr); // here the new modified value gets printed
func3(d2arr);
return 0;
}