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

c# - Update a combobox from a presenter (MVP) -

How to understand 2 main() functions after using uftrace to profile the C++ program? -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -