It seems you might be a bit confused about terms.
An object usually has an address. That is the location in memory where the object is located. Some temporary objects don't have addresses, because they don't need to be stored. Such an exception is the temporary "4" object in the expression (2+2)*3
A pointer is an object that stores an address of another object. Hence, for every type, there is a matching pointer. int
has int*
, std::string
has std::string*
, etcetera.
Now, you write about "addresses of pointers". They exist. After all, I wrote that a pointer is an object, and thus it has its own address. And you can store that address in another pointer. For instance, you can store the address of and int*
in an int* *
. But do you really intended that? Or did you mean the "address referenced by a pointer"?
Now, you give height and weight as examples. The standard way to swap them in C++ is simply std::swap(width, height)
. Note the std::
, which is the prefix for C++ standard library functions. std::swap
will swap almost everything. ints, floats, wives. (j/k).
You have another swap function, apparently. It accepts two pointers to integers, which means it wants the addresses of two integers. Those are easy to provide, in this case. width
is an integer, and &width
is its address. That can be stored in the pointer argument int* a
. Similarly, you can store the address &height
in argument int*b
. Putting it together, you get the call swap(&width, &height);
How does this work? The swap(int*a, int*b)
function has two pointers, holding the address of two integers in memory. So, what it can do is [1] set aside a copy of the first integer, [2] copy the second integer in the memory where the first integer was, and [3] copy the first integer back in the memory where the second integer was. In code:
void swap(int *a, int *b)
{
int temp = *a; // 1
*a = *b; // 2
*b = temp; // 3
}