c - print pointers of type (int *) in a coherent way -
i have code in c:
int tab[10] = {3, 10, 5, 7, 9, 4, 9, 4, 6, 8, 0}; printf("(int*)&tab[0]=%p (int*)&tab[1]=%p (int*)&tab[1]-(int*)&tab[0]=%d\n", (int*)&tab[0], (int*)&tab[1], ((int*)&tab[1]) - ((int*)&tab[0]));
and returns:
(int*)&tab[0]=0xbf9775c0 (int*)&tab[1]=0xbf9775c4 (int*)&tab[1]-(int*)&tab[0]=1
what not understand why difference returned 1 instead of 4 @ end. tell me way print them (addresses , difference) in coherent way (int *)?
because you're doing pointer arithmetic. , pointer arithmetic done in units of whatever pointer pointing (which in case 4, because sizeof(int) == 4
on system).
if want know difference in raw addresses, either multiply result of subtraction sizeof(int)
, or cast pointers char *
before doing subtraction.
Comments
Post a Comment