Does a pointer also have any address or memory allocation?

后端 未结 5 1122
青春惊慌失措
青春惊慌失措 2020-12-31 17:46

If a pointer stores the address of a variable ... then from where do we get the pointer?

What I asked was that if we are using pointer directly, then there must be

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-31 18:48

    Yes, a declared pointer has its own location in memory.

    alt text

    In the example above, you have a variable, 'b', which stores the value "17".

    int b = 17;    /* the value of 'b' is stored at memory location 1462 */
    

    When you create a pointer to that variable, the pointer is stored in its own memory location.

    int *a;
    a = &b;       /* the pointer 'a' is stored at memory location 874 */
    

    It is the compiler's job to know where to "get the pointer." When your source code refers to the pointer 'a', the compiler translates it into -> "whatever address value is stored in memory location 874".

    Note: This diagram isn't technically correct since, in 32-bit systems, both pointers and int's use four bytes each.

提交回复
热议问题