int **p = (int**)new int(5); Confusing !!!
-
Hey Guys, I am trying to execute the following chunk of code. int **p = (int**)new int(5); cout << *p; I am getting output 00000005 As per my understanding , we don't need to typecast return value from new, as it automatically does. Like in the following statement int *ap = new int(20); We don't need to do that, So why I need to do in case of double pointer?? Second thing is, in cout statement, I need to put pointer deference operator once not twice. Even after that i am getting strange output like 00000005 Please someone help me. Thanks Amrit
-
Hey Guys, I am trying to execute the following chunk of code. int **p = (int**)new int(5); cout << *p; I am getting output 00000005 As per my understanding , we don't need to typecast return value from new, as it automatically does. Like in the following statement int *ap = new int(20); We don't need to do that, So why I need to do in case of double pointer?? Second thing is, in cout statement, I need to put pointer deference operator once not twice. Even after that i am getting strange output like 00000005 Please someone help me. Thanks Amrit
Amrit Agr wrote:
So why I need to do in case of double pointer??
Who said you do? This ought to work just fine: ``` int **p = new (int*); *p = new int; **p = 5; cout << **p; delete *p; delete p; ```
The difficult we do right away... ...the impossible takes slightly longer.
-
Hey Guys, I am trying to execute the following chunk of code. int **p = (int**)new int(5); cout << *p; I am getting output 00000005 As per my understanding , we don't need to typecast return value from new, as it automatically does. Like in the following statement int *ap = new int(20); We don't need to do that, So why I need to do in case of double pointer?? Second thing is, in cout statement, I need to put pointer deference operator once not twice. Even after that i am getting strange output like 00000005 Please someone help me. Thanks Amrit
here, p is a pointer to int*.that means p can point to an int pointer and that pointer will point an int type of data. when you do this , (int**) new int (5) ; you are actually forcing (casting) p to point to an int pointer (memory address) with value 5 ..(It actually should not be done because you don't know anything about memory address 0x5 )... however and as you made p to store something(a memory address with value 5) you also allocated memory for that purpose ...... and the typecasting was necessary as first case is very different than the second case.... in the second case --- ap is an int pointer and with new command you allocated 4 bytes of memory for ap .. and ap is now pointing to that memory location and as you passed 20 in the constructor so now that memory address is storing 20.... But, p is not an int pointer ..it's a pointer to int pointer ...so it should be passed a valid memory address in the constructor call .. not an int(what you did -- by passing 5)..that's why you had to cast... and the answer to your last question is ... p is just a pointer (it maybe a pointer to another pointer but it still is a pointer)..and when we want to know what value a pointer is pointing to we put * (deference op.) before that pointer (only once)! Hope,you got that :-D