When we subtract a pointer from another pointer the difference is not equal to how many bytes they are apart but equal to how many integers (if pointing to integers) they are apart. Why so?
Share
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.
The idea is that you’re pointing to blocks of memory
If you have
int* p = &(array[5])then*pwill be 14. Goingp=p-3would make*pbe 17.So if you have
int* p = &(array[5])andint *q = &(array[3]), thenp-qshould be 2, because the pointers are point to memory that are 2 blocks apart.When dealing with raw memory (arrays, lists, maps, etc) draw lots of boxes! It really helps!