The following question was asked in a recent microsoft interview.
What is the difference between the two declarations?
int p=*(int*)i;
int p=*(int*)&i;
I think in the first one i is a pointer and in the second one i is a variable.
Is there anything else?
The first is taking the value contained in
i, treating it as a pointer, and retrieving whateverintvalue is at that address (if possible).The second takes the address of
i, casts it to pointer to int, and retrieves the value at that address. Ifiis anint, it’s equivalent top=i;. If it’s not, it’s going to take the firstCHAR_BIT *sizeof(int)bits starting at the address ofi, and (attempt to) treat them as anint, and assign whatever value that creates top.Edit: and yes, as @R. Martinho Fernandes pointed out, if
ihas an overloadedoperator &, it may do something rather different from any of the above (i.e., instead of the address ofiit’ll start with whatever itsoperator &returns).