C pointer arithmetic while subtracting -


int a[ ] ={1,3,5,7,9,11,13,15,17,19};     int *pa = &a[4],*pb = &a[1]; 

what value of pa-pb? ans: 3, how? shouldn't 12 (like printed pa , pb too, double check, , got 12)?

pointer arithmetic different normal arithmetic. in pointer arithmetic a-1, a pointer doesn't mean value of a subtracted 1. means go 1 unit of memory.

in example, pa-pb doesn't mean value of pa-pb. 1 int variable requires 4 bytes pa-pb means go 3 int's reach pb pa.

for int 1 unit of memory 4 bytes, float 8 bytes, char 1 byte. note: these value changes system system, these value.


Comments

Popular posts from this blog

Retrieving ETA (estimated time of arrival) with Google Distance Matrix API and public transit as transport mode -

javascript - Confirm a form & display message if form is valid with JQuery -

ionic framework - Meteor - Error: Failed to execute 'insertBefore' on 'Node' -