Does a pointer also have any address or memory allocation?

后端 未结 5 1133
青春惊慌失措
青春惊慌失措 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条回答
  •  不知归路
    2020-12-31 18:30

    The compiler deals with translating the variables in our code into memory locations used in machine instructions.

    The location of a pointer variable depends on where it is declared in the code, but programmers usually don't have to deal with that directly.

    A variable declared inside a function lives on the stack or in a register, (unless it is declared static).

    A variable declared at the top level lives in a section of memory at the top of the program.

    A variable declared as part of a dynamically allocated struct or array lives on the heap.

    The & operator returns the memory location of the variable, but unlike the * operator, it can't be repeated.

    For example, ***i gets the value at the address **i, which is the value at address *i, which is the value stored in i, which the compiler figures out how to find.

    But &&i won't compile. &i is a number, which is the memory location the compiler uses for the variable i. This number is not stored anywhere, so &&i makes no sense.

    (Note that if &i is used in the source code, then the compiler can't store i in a register.)

提交回复
热议问题