Can anyone know why this code is returning 870 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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; }

26th Nov 2019, 3:50 AM
Preity
Preity - avatar
3 Answers
+ 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
26th Nov 2019, 3:57 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 3
Preity first solve (*y) and then apply the pre decrement operator. So 30 * --(30) = 30 * 29 = 870
26th Nov 2019, 4:22 AM
Avinesh
Avinesh - avatar
0
Preity These questions are asked in gate exams??🤔
26th Nov 2019, 5:26 AM
Alaska
Alaska - avatar