+ 2
Can anyone know why this code is returning 870
#include <iostream> using namespace std; int square(int *x){ *x = (*x)++ * (*x); } int square(int *x, int *y){ *x = (*x) * --(*y); } int main() { int number = 30; square(&number,&number); cout<<number; return 0; }
3 Respuestas
+ 5
Preity square method with two arguments will be called as you have passed twice from main function. This is case of pointer as you have passed address of number from main function. For square method, both x and y refer to number address only..
First of all , *x is 30 and *y is 30. Now -- of *y makes it 29 so, 30*29 = 870 is assigned to x which points to number... Hence, output is 870.
Hih
+ 3
Preity first solve (*y) and then apply the pre decrement operator.
So 30 * --(30) = 30 * 29 = 870
0
Preity
These questions are asked in gate exams??🤔