0

C++, Plz explain this program logic to me

Int* function (int* p){ P+=2; Return p; } Int main(){ Int array[5]={1,2,3,4,5}; Int* ptr= array; Cout<< *(function (ptr)); Return 0; }

27th Jun 2021, 11:16 AM
Nitin Bisht
Nitin Bisht - avatar
3 Respostas
+ 4
Int* ptr stores the address of first array element . At line Int* function (int* ptr); //int*ptr accepts the address . p+=2; // increments the address by 2*sizeof(int) =8 . If the initial address was 104 next address will be 112 (which will basically have array number 3 stored at it ) . return p; // returns the address as the type of function is Int* . At line *(function (ptr)) ; //* dereference the address and returns the value(3) stored at it
27th Jun 2021, 11:34 AM
Abhay
Abhay - avatar
+ 3
Nitin Bisht it is not a pointer to function . int* is the return type of function here like int, void, string type. And if i am not wrong we can put a pointer to a function but that has a different syntax and i can't help with that.
27th Jun 2021, 11:50 AM
Abhay
Abhay - avatar
0
Abhay can we put pointer to a function I have never face this type of program before ? Int* function (int* p)
27th Jun 2021, 11:44 AM
Nitin Bisht
Nitin Bisht - avatar