Exchange pointer addresses in C ++

How can I exchange pointer addresses inside a function with a signature?

Say:

int weight, height; void swap(int* a, int* b); 

So, after exiting this function, the addresses of real parameters ( weight and height ) will be changed. Is it possible at all?

+4
source share
5 answers

If you want to swap the places pointed to by pointers, and not just the values ​​stored at this address, you need to pass the pointers by reference (or a pointer to a pointer).

 #include <cassert> void swap(int*& a, int*& b) { int* c = a; a = b; b = c; } int main() { int a, b; int* pa = &a; int* pb = &b; swap(pa, pb); assert(pa == &b); //pa now stores the address of b assert(pb == &a); //pb now stores the address of a } 

Or you can use the STL replacement function and pass pointers to it.

 #include <algorithm> std::swap(pa, pb); 

However, your question is not very clear.

+14
source

New answer (as the question was reformulated)

- this is that the addressing of variables is determined at compile time and therefore cannot be replaced. Pointers to variables, however, can be replaced.

Old answer: this was the answer when the question still involved replacing the values ​​of two variables with a function:

function call:

  int width=10, height=20; swap(&width, &height) 

implementation:

  void swap(int *a, int *b) { int temp; temp=*a; *a = *b; *b = temp; } 

or ... without using a temporary variable :; ^)

  void swap(int *a, int *b) { *a ^= *b; *b ^= *a; *a ^= *b; } 

remember that the last method breaks down for the case: swap (& a, & a). Very sharply pointed user9876 in the comments.

+8
source

In C ++ you write

  void swap(int *a, int *b) { std::swap(*a, *b); } 

or rather just use:

  std::swap(a, b); 
+8
source

It seems you can confuse the conditions a bit.

Usually an object has an address. This is the location in memory where the object is located. Some temporary objects do not have addresses because they do not need to be stored. Such an exception is a temporary β€œ4” object in the expression (2+2)*3

A pointer is an object that stores the address of another object. Therefore, for each type there is a corresponding pointer. int has int* , std::string has std::string* , etc.

Now you are writing about "pointer addresses". They exist. In the end, I wrote that a pointer is an object, and therefore it has its own address. And you can save this address in another pointer. For example, you can save the address and int* to int* * . But did you really intend to do this? Or did you mean "the address to which the pointer refers "?

Now you give height and weight as examples. The standard way to replace them with C ++ is simply std::swap(width, height) . Pay attention to std:: , which is a prefix for standard C ++ library functions. std::swap will replace almost everything. ints, floats, wives. (C / C).

You have another swap function, apparently. It takes two pointers to integers, which means that it wants the addresses of two integers. In this case, they are easy to provide. width is an integer, and &width is its address. This can be stored in the pointer argument int* a . Similarly, you can store the address &height in the int*b argument. By connecting it, you get a call swap(&width, &height);

How it works? The function swap(int*a, int*b) b) has two pointer address holding two integers in memory. So what he can do is [1] select a copy of the first integer, [2] copy the second integer to the memory where the first integer was, and [3] copy the first integer back to the memory, where the second integer. In code:

  void swap(int *a, int *b) { int temp = *a; // 1 *a = *b; // 2 *b = temp; // 3 } 
+6
source

If you want to change the address of the pointers, you need to pass the pointers of these pointers as parameters:

 void swap(int **a, int **b); //as prototype 

These examples simply change the values ​​of pointers.

+1
source

All Articles