开发者

Memory address of a memory address in a machine? (C language)

Today out of curiosity, i tried something very weird:

The Code :

int num = 2;
int * point = #

printf("%p\n" , &point);

The Question :

1) The first thing i do is i append the address of variable num to pointer point , after that i use printf() to print out the address stores in the pointer point.

2) Later , i just try to modify the code(Above Code) to see what behavior it will gives , that is by using the printf() to print out the address of pointer point instead of printing out the address stores in pointer point which is the address of variable num.

3) Still it will print me the address although the addres开发者_如何学运维s is different to the address of the variable num , just wanna know does this behavior is well defined in C standard??Why does a pointer has its own address too because i thought its job is to store other variable address.

Thanks for spending time reading this.


A pointer is just another variable, and hence also needs a place to live.


|----------|                             
|   point  |                            
|          |  1000                       
|   2000   |                             
|----------|                           
      |        
      |
      |
      |
      -------------------->|----------|
                           |   num    |
                           |          |  2000
                           |    2     |     
                           |----------|

The pointer point itself is also a variable and it needs some space in the memory to reside at. As shown above it points/stores the memory location where num is stored (2000) in memory, but for point to exist it itself needs a separate memory location (1000).


Everything in your computer is ones and zeroes. Just as an int variable is just ones and zeroes at a specific memory location, a pointer is also just that. The difference is how your program decides to threat the contents of that memory cell. If you declare a pointer, it will use the memory cell to store the address of some data, yet the memory cell where that is stored must also have an address, like everything else.

This isn't so much about the C language, but rather about how a computer works.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜