What's the difference between a pointer, and a pointer variable?

前端 未结 6 1546
执笔经年
执笔经年 2021-02-04 10:50

I know this is very basic but it is little bit confusing to me.
I\'ve read:

a pointer is nothing more than an address

6条回答
  •  粉色の甜心
    2021-02-04 11:19

    Let's replace the word "pointer" with a datatype that's hopefully more familiar, like an int:

    int n = 42;
    

    Here 42 is an int value, and n is a variable that holds an int. You could call n an "int variable". An int is a number like 42 or -25315685, and an int variable holds these numbers. Once you have a variable you can assign different numbers to it. Nothing confusing so far?

    A pointer is just like an int: a number. It happens to be a number that identifies a memory location, and if something is stored in that memory location you can call it an address. Like an int, a pointer can be stored in a variable. A variable that stores a pointer could be called a pointer variable.

    So, what's the difference between a pointer and a pointer variable? The first one is a value, like a number, the second stores these values. But often people refer to variables by their values that they store; people don't call n an "int variable" but just int, even though it can at different times store different ints. In this text I'll do the same and sometimes write pointer when I mean a pointer variable; hopefully the distinction will be clear.

    Is a pointer always an address? This is a question about the meaning of the word "address" more than anything else. A pointer is always an address in the sense that it corresponds to a memory location in one way or another, it's an "address" for that memory location. But on the other hand, if the memory location is not accessible to the program or doesn't have anything useful stored in it, is it really an "address" for anything?

    Let's now investigate the following code:

    int *p;
    p = &n;
    

    The first line declares a pointer variable called p. The pointers that can be stored into p are memory locations for int data; this is important for reasons that we'll see later. We still don't give p any value, so the pointer it stores is arbitrary. It certainly doesn't store the address of anything useful; it may even point to an area of memory inaccessible to the program.

    In the second line we take the address of the n variable with the &-operator and assign it to p. Now p stores the address of n, the memory location where the value of n is stored.

    What can we do with a pointer? We can read and write to the memory location that the pointer identifies. To do this we "dereference" the pointer with the *-operator, and then (*p) can be used just like you can use n. For example, you can write a new value into n with this:

    *p = 123;
    

    It's at this point that it becomes apparent why we need to know the type of data that p can point to: otherwise you can't know what you could assign to (*p).

    Another reason why it's important to know the type of data that p can point to is pointer arithmetic. For example p+1 is a pointer to the int stored in memory right after n; if p was a pointer to a big data structure p+1 would be a pointer to a data structure of the same type stored right after it. For this the compiler must know the size of what the pointer points to.

提交回复
热议问题