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 variablei
in the pointer variablep
, we say thatp
points toi
.
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 whichp
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?
A pointer to a pointer is a form of multiple indirection, or a chain of pointers. Normally, a pointer contains the address of a variable. When we define a pointer to a pointer, the first pointer contains the address of the second pointer, which points to the location that contains the actual value as shown below.
A pointer is a variable that stores a memory address. Pointers are used to store the addresses of other variables or memory items. Pointers are very useful for another type of parameter passing, usually referred to as Pass By Address. Pointers are essential for dynamic memory allocation.
A user creates a pointer for storing the address of any given array. A user creates an array of pointers that basically acts as an array of multiple pointer variables. It is alternatively known as an array pointer. These are alternatively known as pointer arrays.
Pointer is a data type and the purest form of it in C is void *. A void * can pass the memory address around which is what a pointer does but it cannot be dereferenced. Dereferencing means to get at the data contained at the memory location the pointer is pointing at.
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 = %d\n", v1, v2);
swap_int(&v1, p2);
printf("v1 = %d, v2 = %d\n", 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With