What's the difference between a pointer, and a pointer variable?
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 , and a pointer variable is just a variable that can store an address .
When we store the address of a variable i
in the pointer variable p
, we say that p
points to i
.
int i, *p = &i;
p
points to i
.
To gain access to the object that a pointer points to, we use the *
(indirection) operator.
If p
is a pointer then *p
represents the object to which p
currently points.
Now I am confused that what should I call p
-- pointer or pointer variable ?
Additional: Is a pointer always the same as an address?
The difference between a pointer value and a pointer variable is illustrated by:
int swap_int(int *i1, int *i2)
{
int t = *i1;
*i1 = *i2;
*i2 = t;
}
int main(void)
{
int v1 = 0;
int v2 = 37;
int *p2 = &v2;
printf("v1 = %d, v2 = %dn", v1, v2);
swap_int(&v1, p2);
printf("v1 = %d, v2 = %dn", v1, v2);
return 0;
}
Here, p2
is a pointer variable; it is a pointer to int
. On the other hand, in the call to swap_int()
, the argument &v1
is a pointer value, but it is in no sense a pointer variable (in the calling function). It is a pointer to a variable (and that variable is v1
), but simply writing &v1
is not creating a pointer variable. Inside the called function, the value of the pointer &v1
is assigned to the local pointer variable i1
, and the value of the pointer variable p2
is assigned to the local pointer variable i2
, but that's not the same as saying &v1
is a pointer variable (because it isn't a pointer variable; it is simply a pointer value).
However, for many purposes, the distinction is blurred. People would say ' p2
is a pointer' and that's true enough; it is a pointer variable, and its value is a pointer value, and *p2
is the value of the object that p2
points to. You get the same blurring with ' v1
is an int
'; it is actually an int
variable and its value is an int
value.
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 int
s. 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.
p
is a pointer variable, that points to a variable i
. We can simply call it pointer.
A declaration:
int* p;
int i;
p = &i;
declares p
as the identifier of an int
type object. This is usually stated more succinctly as 'p is a pointer to i'
. p
can be use to refer int variable i
after expression p = &i
. To access value of variable i
using pointer p
you can use Dereference operator *
(eg *p
). Anf i = 10;
equivalent to *p = 10;
.
Also notice in expression p = &i;
to read address of i
I used &
ampersand operator also called Address of operand
.
Pointer is just a logical address (a identifier by which a variable can be referenced). The C standard does not define what a pointer is internally and how it works internally.
You would like to read: What exactly is a C pointer if not a memory address?
Additionally, read this: to Understand the purpose of pointers.
上一篇: 指向数组/指针消除歧义数组的C指针
下一篇: 指针和指针变量有什么区别?