Powered By Blogger

Wednesday 27 April 2011

Addition of a digit to the elements of an array using pointer arithmetic, while the user defined array is passed to the called function using pointer.


#include<stdio.h>
void pass(int *);
int main(void)
{
int *array,i=0,*a;
int b=10;
array=(int*)calloc(sizeof(int)*b);
printf("Enter the %d elements of the array :: ",b);
for(i=0;i<10;i++)
{
scanf(" %d",&array[i]);
}
a=array;
pass(a);
for(i=0;i<10;i++)
{
printf("%d ",array[i]);
}
return 0;
}

void pass(int *passed)
{
int *p,i=0,s=0;
printf("\n\nEnter a digit to add to each of the element of the array::");
scanf("%d",&s);
printf("\n\nAfter addition using pointer arithmetic::\n\n");
for(p=passed,i=0;i<10;i++,p++)
{
*p=*p+s;
}
printf("\n");

}


No comments:

Post a Comment