-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray_as_parameter.c
31 lines (30 loc) · 947 Bytes
/
array_as_parameter.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// A basic C program to see how the function returns an array and how the array is passed as parameter from one function to another. Arrays are always passed // by address, they are never passed by value or passed by reference.
#include <stdio.h>
#include <stdlib.h>
int * fun(int *A,int *B,int n)
{
int *C=(int*)malloc(sizeof(int)*n);
for(int i=0;i<n;i++)
C[i]=A[i]+B[i];
return C;
}
int main()
{
int n;
printf("Enter the number of elements in the array \n ");
scanf("%d",&n);
int A[n],B[n];
int *C;
printf("\nEnter the elements of the first array : \n");
for(int i=0;i<n;i++)
scanf("%d",&A[i]);
printf("\nEnter the elements of the second array : \n");
for(int i=0;i<n;i++)
scanf("%d",&B[i]);
printf("\nThe array of sum of the corresponding elements of both arrays is given by : \n");
C=fun(A,B,n);
for(int i=0;i<n;i++)
printf("%d ",C[i]);
free(C);
return 0;
}