简体   繁体   中英

Why is the difference between the addresses of int and a char is 16?

I declared an integer (32bits) and a char (8), and I know that a computer address is 1 byte at a time, so why is the difference between those two addresses is 16 and not let's say 40? if each memory address is 8 bits and the size of an integer is 32, then the range is expected to be greater than 16, no?

int main(void)
{
    char a = 'g';
    int b = 5;
    printf("%p %p\n", &a, &b);

    return 0;
}
(gdb) p 0x7fffadfcc2c0 (a) - 0x7fffadfcc2b0 (b)
$1 = 16

The difference between memory addresses is in bytes, not bits.

The minimum difference between the addresses of these two variables is 1, if the address of a is less than the address of b, because the size of a is 1 byte.

The minimum difference is 4, if the address of b is less than the address of a, because the size of b is 4 bytes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM