I'm very confused why this code works. I was taught that a pointer was a "capsule" holding the address of something. So the swapnum
function should be expecting one of those "capsules", not the actual address of the int. Is it creating a temporary pointer and setting it to that address? If that's the case, then what would be happening if I passed a pointer in by reference such as int * c = &a;
swapnum (&c...
?
#include <stdio.h>
void swapnum(int *i, int *j) {
int temp = *i;
*i = *j;
*j = temp;
}
int main(void) {
int a = 10;
int b = 20;
swapnum(&a, &b);
printf("A is %d and B is %d\n", a, b);
return 0;
}
pointer
is just another word for address. There's nothing really magic about it, it is just a variable that contains a number, that number being the memory address of the thing it points to.
In your example, memory might look like this:
0x00001004 - 10 # a
0x00001008 - 20 # b
0x0000100C - 0x0001004 # i
0x00001010 - 0x0001008 # j
0x00001014 - 00 # temp
i
and j
are both int*
which means they contain an address that contains an integar. The code *i
says "return whatever value is at the address in i
and assume it is an int
.
So if you look at your swapnum
, here's what happens:
int temp = *i;
# take the value at 0x0001004 (10) and place it in 0x00001014
*i = *j;
# place the value at 0x0001008 (20) and place it in 0x00001004
*j = temp;
# place the value at 0x0001014 (10) and place it in 0x0001008
After which memory looks like this:
0x00001004 - 20 # a
0x00001008 - 10 # b
0x0000100C - 0x0001004 # i
0x00001010 - 0x0001008 # j
0x00001014 - 10 # temp
You could certainly do this:
int* c = &a;
swapnum(c, &b);
All you are doing is getting the address of a
, putting it in c
and then passing it into swapnum
, which expects an address. A pointer is just an address.
The address of an object is a pointer to the object! That's really all there is to pointers, actually: they now at which address an object lives. The whole point of using addresses instead of objects is that using addresses you have the same object.
&item
returns a pointer to item
.
Also, on all modern architectures I know of a pointer is simply the address of the item.
If you make a pointer to a pointer and pass it to swapnum one of two things will happen:
In main
you're using the address-of &
operator to pass to swapnum
the addresses of a
and b
.
The value of a pointer actually is an address. There's no "capsule" at all! (Maybe, it's even easier to think of them as plain addresses without capsules!).
If you did int *c = &a
you could pass c to swapnum()
instead of &a
and it would work just fine. You can't pass &c
because you'd be passing an int **
, while the function is expecting an int *
.
&a
is an expression which is evaluated as the a's
address in memory. This value must be stored in a variable of type pointer. For example:
int* i = &a;
Here, i
is a variable of type int pointer
which acts as a container, a capsule, for the value assigned to it. However, this capsule doesn't keep the a
value itself; it keeps the &a
value. Also, being a capsule is a direct result of being a variable in general; not necessarily because of being a variable of pointer
type.
Now, this statement:
swapnum(&a, &b);
does a very normal job of coping the arguments into some local variables - a normal pass-by-value function call. In this case, what actually happens is it copies the &a
value, ie the a's
address, into a local variable i
, which of course, is required to be defined of type int pointer
.
if you defined:
int * c = &a;
and then call:
swapnum (&c...
that wouldn't work because c
would be a variable of type int pointer
; &c
, then, would be the address of such a variable; Therefore, the local variable type i
which would contain this address should have been defined as a container for such type of value; ie it should have been defined as a variable of type pointer to the address of an int pointer
or int **
Such a function call without changing the function correspondingly would certainly result in a compiler time error.
C++ is a statically typed language. It means that every expression has one specific type known compile-time. The C++ standard tells us that if the expression expr is of built-in type T then the expression &expr is of type T* (pointer to T). That's all...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.