14.13 – Sending address of array as argument

by subbu on January 26, 2014

External pointer to an array

In the previous session, we have learned that the name of array gives the address of base element but, it is not a pointer hence pointer arithmetic can’t be performed with the name of array.

Now we can assign the address of an array to another pointer so that, pointer arithmetic can be performed using external pointer.

short a[]={12,45,44,23,54,21};
short *p=a;

external pointer to array

Here a gives the address of array, which is assigned to a short type of pointer p. Now p is a pointer to array and free to participate in pointer arithmetic because it can be incremented and decremented.

for(i=0;i<5;i++)
{
 printf("%5d",*p);   /* accessing elements using dereferencing */
 p++;                     /* incrementing pointer by 1                    */
}

p refers the next elements address every time p is incremented by 1 using p++.  *p access the current element’s value

#include<stdio.h>
int main()
{
short a[]={20,30,40,50,60},i;
short *p=a;
printf("Elements of array:\n");
for(i=0;i<5;i++)
{
 printf("%5d",*p);
 p++;
}
return 0;
}

Output:
Elements of array:
20   30   40   50   60

Sending the address of an array as argument

In the previous session “9.10 sending array as argument” while sending an array as argument, the name of array is written as actual argument and an array is defined as a formal argument. We have observed that, change in formal argument has changed the elements of actual argument.

short x[]={12,34,54,55,62,67};
process(x,6);

Here x is an array, sending as argument to the function process()

void process(short y[],int n)
{
short i;
for(i=0;i<n;i++)
  y[i]=y[i]+10;
}

Here in the function process () changing every element of array y[] by 10 which results the change in actual argument x[].

#include<stdio.h>
void process(short[],int);
int main()
{
short x[]={12,34,54,55,62,67};
short i;
process(x,6);
printf("Elements of array:\n");
for(i=0;i<6;i++)
  printf("%5d",x[i]);
return 0;
}
void process(short y[],int n)
{
short i;
for(i=0;i<n;i++)
  y[i]=y[i]+10;
}

Output:
Elements of array:
22   44   64   65   72   77

Now we will see why it happens

When we write the name of array as actual argument, it is the address of array that is being passed as argument. The formal argument must be a pointer to store the address. Now the pointer could refer every element of array from the called by function using either pointer or address arithmetic

#include<stdio.h>
void process(short[],int);
int main()
{
short x[]={12,34,54,55,62,67};
short i;
process(x,6);
printf("Elements of array:\n");
for(i=0;i<6;i++)
  printf("%5d",x[i]);
return 0;
}
void process(short *p,int n)
{
short i;
for(i=0;i<n;i++)
  *(p+i)=*(p+i)+10;
}

Output:
Elements of array:
22   44   64   65   72   77

sending the address of array as argument

p is the pointer to array x[]. Accessing and changing the elements of x[] by it’s pointer p from the called by function using dereferencing *(p+i)

The function process() can be written in different forms like

void process(short *p,int n)
{
short i;
for(i=0;i<n;i++)
  p[i]=p[i]+10;
}

Or

void process(short p[],int n)
{
short i;
for(i=0;i<n;i++)
   *(p+i)=*(p+i)+10;
}

Or

void process(short p[],int n)
{
short i;
for(i=0;i<n;i++)
   p[i]=p[i]+10;
}

The meaning of all the functions is same, the formal parameter in the function definition

short *p;

and

short p[];

are equal.

The first version is preferable because it clearly says that, p is a pointer to array.  Even the meaning of second version is appropriate whose inner meaning is

short *p;

Conclusion:

While sending an array as argument, the formal parameter can be written as either *p Or p[]. The inner meaning of p[] is *p. That is why change in formal argument results change in actual argument in case of array.

Previous post:

Next post: