I have this little Program:
#import <stdio.h>
#import <stdlib.h>
void main(void) {
char a;
char b;
printf("Adress a: %p\n", (void *)&a);
printf("Adress b: %p\n", (void *)&b);
return 0;
}
The adress of b is lower, than the adress of b. Why is it like this? Or am i doing something wrong?
The storage space for a local variable is on the stack. The X86 processor family has a stack which "grows downward". This means that as allocations occur (for example assigning a variable), the stack pointer is moved downwards toward lower memory addresses.
&a
is greater than &b
because after &a
was assigned, the stack pointer was moved downward to a lower address for the next allocation.
In your case the stack grows down.
a
and b
are allocated on the stack in order of definition. So you have that &a
is higher than &b
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With