In pointers there are two arithmetic operations which can be performed. One is addition and second is subtraction. When you add some numbers in pointer it means that you are moving forward in memory and when you subtract some numbers then it means you are traversing back from that address where pointer is pointer at . You cannot divide or multiply in pointer arithmetic because it does not give you any logical meaning that you are dividing two different addresses or multiplying two addresses.

Let’s see how this arithmetic works.

We start with example,

int i = 20;

int *pi = &i;

In first line integer i is initialized with value 20 and in second line pointer to integer pi is initialized with address of i. Suppose starting location of i is 301 and pointer pi is pointing the starting location of integer i. We know that size of integer is 4 bytes so from 301 to 304 , 4 bytes are occupied by i in memory.

pi = pi +1;

if you add value 1 in pi then what will be the current value of pi or on which memory location the pointer pi will be pointing at. The answer is 305. Yes this is bit strange answer in pointer arithmetic.

Rule is very simple, **
The pointer will skip the bytes, according to the data type it points to**. In our case pi is pointer to integer and size of integer is 4 bytes. When we increment in pi then it will skip 4 bytes in memory and pi will be pointing to memory location 305.

char *pc;

pc = pc + 1

if pc is pointer to character and character size is 1 byte. If you add 1 in pc then in this case only 1 byte will be skipped because of the character size. if you have pointer to double (double *pd) then in this case 8 bytes will be skipped because size of double is 8 bytes.

It is same in pointer subtraction. If pi is pointer to integer and it is pointing at memory location 301 then pi = pi – 1 will skip 4 bytes in reverse and the new value of pi will be 297. This is all about pointer arithmetic.

At the end of this topic I want to tell you an interesting point which we have discussed but it is not part of this topic. When you initialize any variable in memory and assign it with some value then this variable occupies some space in memory, in our case integer i has occupied 4 bytes from 301 to 304. When you initialize pi pointer to integer with address of i pi = &i, then from out of 4 bytes, on which value pointer pi will be pointing at. The answer is the address of lowest byte. In memory if the integer is initialized from 301 to 304 then pointer pi will be pointing at 301.

Website

Wow, this post is fastidious, my younger sister is analyzing these things, so I am going to tell her.|

Website

I’m no longer sure where you’re getting your info, however great topic. I needs to spend some time finding out much more or working out more. Thanks for great info I was searching for this information for my mission.|

Website

What i do not realize is in reality how you are now not really a lot more smartly-favored than you may be now. You are so intelligent. You know thus considerably in the case of this topic, made me in my view imagine it from a lot of numerous angles. Its like men and women aren’t involved until it is one thing to do with Girl gaga! Your individual stuffs great. Always deal with it up!|