14.14 – Return by address of an array

by subbu on January 27, 2014

Return by array address

As we send the address of an array as argument to the function, we can also return the address of an array to the calling function.

When we write the name of array with the return statement, it is the address that will be returned to the caller, the caller will have pointer to array. So that the caller could access the elements of array either by using the subscript or by using address arithmetic.

return by array address

#include<stdio.h>
short* source();
int main()
{
short *p,i;
p=source();                               /* getting pointer to array   */
printf("Elements of array:\n");
/* for(i=0;i<5;i++)
  printf("%5d",*(p+i)); */
for(i=0;i<5;i++)
  printf("%5d",p[i]);
return 0;
}
short* source()
{
short x[]={12,34,56,66,77};
return x;                                  /* returning the address of array */
}

Output:
Elements of array:
3915  205  230 4250  -10  700

Though the program is successfully executed, the output is garbage value because array scope ends once the address is returned and function execution is completed. We can expand the scope of array by defining it as a static array.

#include<stdio.h>
short* source();
int main()
{
short *p,i;
p=source();                               /* pointer to array   */
printf("Elements of array:\n");
/* for(i=0;i<5;i++)
  printf("%5d",*(p+i)); */
for(i=0;i<5;i++)
  printf("%5d",p[i]);
return 0;
}
short* source()
{
static short x[]={12,34,56,66,77};
return x;                                  /* returning the address of array */
}

Output:
Elements of array:
12   34   56   66   77   88

Specification: Accept two vectors of same size and print the sum of them using the sub function vector_sum()

#include<stdio.h>
short* vector_sum(short*,short*,short);
int main()
{
short x[20],y[20],*s;
short n,i;
printf("Enter the size of vectors:");
scanf("%d",&n);
printf("Enter %d elements into 1st vector:\n",n);
/* accepting elements into first vector */
for(i=0;i<n;i++)
  scanf("%d",&x[i]);
printf("Enter %d elements into 2nd vector:\n",n);
/* accepting elements into second vector */
for(i=0;i<n;i++)
  scanf("%d",&y[i]);
s=vector_sum(x,y,n);                 /* sending arrays as arguments */
/* s is the pointer to array sum[] */
printf("Sum of two vectors:\n");
for(i=0;i<n;i++)
  printf("%5d",s[i]);
return 0;
}
short* vector_sum(short *p,short *q,short n) /* p, q are pointers to arrays */
{
static short sum[20];
short i;
for(i=0;i<n;i++)
  sum[i]=p[i]+q[i];
return sum;                           /* returning the address of array (sum) */
}

Execution:
Enter the size of vectors:5
Enter 5 elements into 1st vector:
12 34 15 66 10
Enter 5 elements into 2nd vector:
55 34 12 44 20
Sum of two vectors:
67   68   27  110   30
x, y are arrays of caller, addresses of which are sent as arguments to the function vector_sum()

s=vector_sum(x,y,n);

p, q are the pointers to the arrays x, y; Addition of x, y is stored in another static array sum[] of sub function.

short* vector_sum(short *p,short *q,short n) /* p, q are pointers to arrays */
{
static short sum[20];
short i;
for(i=0;i<n;i++)                    /* performing vector addition */
  sum[i]=p[i]+q[i];
return sum;                           /* returning the address of array (sum) */
}

Address of sum is returned to the caller and assigned to the pointer s. So, s would be the pointer to sum[] in the caller. Now s could access the elements of sum[] from the caller

s=vector_sum(x,y,n);
/* s is the pointer to array sum[] */
printf("Sum of two vectors:\n");
for(i=0;i<n;i++)
  printf("%5d",s[i]);

Previous post:

Next post: