By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,471 Members | 722 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,471 IT Pros & Developers. It's quick & easy.

Pointer's subtraction or arithmetic

P: 1
Can we subtract a variable value from a pointer
Means is this possible *P-t where t is a variable
Apr 25 '16 #1
Share this Question
Share on Google+
1 Reply


weaknessforcats
Expert Mod 5K+
P: 9,197
Yes.

But a pointer has a type so ptr + 1 will take the address in the pointer and the sizeof one int to it.

ptr + 5 where the type of ptr is double will take the address in ptr and the sizeof 5 doubles to it.

That is, doing arithmetic with a pointer is not just adding a number of bytes.

This leads to the rule that a pointer changes by the sizeof the thing it points at.

Your example, however, *P -t does not change the pointer. It changes the thing the pointer points at.
Apr 25 '16 #2

Post your reply

Sign in to post your reply or Sign up for a free account.