Tuesday, 12 June 2012

Function returning pointer to array


Function returning pointer to array in c programming.

char (*pa())[4];
void main(){
    char(*p)[4]=pa();
    clrscr();
    printf("%d",**p);
    getch();
}
char (*pa())[4]{
       static char arr[]={'\11','\12','\13','\14'};
       return &arr;
}
Output: 9


ONE MORE EXAMPLE:

int* insertion_sort();
void main(){
int *arr,i;
arr=insertion_sort();
clrscr();
for(i=0;i<=7;i++)
printf("%d ",arr[i]);
getch();
}
int * insertion_sort(){
static int arr[]={3,11,2,0,5,23,8,1};
int i,j,temp,s=8;
for(i=1;i<=s-1;i++)
{
temp=arr[i];
j=i-1;
while((temp<=arr[j]-1)&&(j>=0))
{
arr[j+1]=arr[j];
j=j-1;
}
arr[j+1]=temp;
}
return arr;
}
Output: 0 1 2 3 5 8 11 23

No comments:

Post a Comment