Thứ Ba, 2 tháng 9, 2014

Returning Pointer to pointer from a function...

Actually this question asked in TCS technical round interview.We can return a pointer to  pointer from a function .

am giving a sample code for it to demonstrate process..

sample code
----------------------------
#include<stdio.h>
#include<conio.h>
int** datavalue();
int main()
{
clrscr();
int **ptr;
ptr=datavalue();
printf("%d",++**ptr+3);
return 0;
}
int** datavalue()
{
    int *p,**ptr;
    int arr[]={1,2,3,4,5,6,7};
    p=arr;
    ptr=&p;
    return ptr;

}

output
=============
5

Explanatition:
ptr is a pointer to pointer so function must return double pointer(so am taken a **ptr ).
**ptr points to first value in array ,if we says like **ptr+3 it points to the 4th element (arry indices starts from 0),if e say like ++**ptr+3 it increase the value of (**ptr+3) i'e ++4 ,now ans becomes 5.
 

 


Không có nhận xét nào:

Đăng nhận xét