0

Output question

C++ #include <iostream> using namespace std; int main() { int mas[9]={2,6,3,9,1,0,5,8,4}; int *p=new int[9]; for(int i=0;i<9;i++){ p[i]=mas[i]; } int n1=sizeof(mas)/sizeof(*mas); int n2=sizeof(p)/sizeof(*p); cout << n1 <<n2; return 0; The answer is 92 . But I have doubt in n2 how it is 2 the sizeof(*p) is 4 so for being answer 2 , sizeof(p) should be 8. If possible then please explain me .

15th Oct 2021, 1:54 PM
Achintya Raj
Achintya Raj - avatar
4 Respuestas
+ 2
I hope you understand the silliness of the line int *p=new int[9]; and why you get a warning there. If you divide 8/4, the answer is 2. Are you expecting a different answer?
15th Oct 2021, 2:41 PM
Brian
Brian - avatar
+ 2
Martin Taylor excellent answer! I was thinking the declaration was "silly" because it had unintended consequences in the calculations using sizeof. I had in mind the warning would be eliminated with a compile-time allocation, int p[9];, but your solution fits most flexibly with possible original intents of the author. Kudos!
15th Oct 2021, 6:14 PM
Brian
Brian - avatar