I have 2 forms which actually output different results which I dont understand
int x = *(int*)&M[index]
compared to
int x = (int)M[index]
Can someone explain to me what is the difference?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If the datatype of M is not int[], they may differ, take the case where you have an array of 2 (16 bit) shorts, 0x0123 and 0x4567. The memory layout (big endian) would/may be;
In the latter case with M[0], you’d assign the short correctly to the integer since you’d read M[0] and convert it to an integer, 0x00000123
In the first case you’d take the address of M[0] and assume that what’s in memory there is an integer, that’d with a 32 bit integer type give the result 0x01234567 (ie all 4 bytes would be read)