Best explanation of C ++ pointer function?

I am working on a problem for my homework in C ++ that includes pointers. I do not ask anyone to do my work, but just help to understand the problem.

The challenge is to write a function

void sort2(double* p, double* p) //First off I am assuming the second "p" is a typo and should be aq 

This function gets two pointers and sorts the values ​​that they point to. For example, if you call sort2(&x, &y) , then x <= y after the call.

What is interesting to me if the pointers are already canceled by the function already, I just compare x and y, and not their memory addresses? If not, how do I compare memory addresses?

+6
source share
6 answers

You pass the addresses of the variables 2x double , so the function sort2() can change the values ​​in the source.

Thus, when the sort function is called like this:

 sort2(&x, &y) 

The function can access the caller's memory addresses for x and y and change the values ​​stored there, if necessary.

However, if the function simply took double parameters by value, i.e.

 void sort2(double p1, double p2) 

then, although the function can still compare and change values ​​around, there is no way for sort2() to pass BOTH new values ​​back to its caller with its current signature, since p1 and p2 copied by value to the stack and will be lost when the function returns.

The sort function accesses the values ​​of pointer variables by deleting their references, for example.

 if (*p1 > *p2) { // swap / sort etc. double swap = *p2; *p2 = *p1; *p1 = swap; } 

And yes, you're right, the second p almost certainly a typo.

+4
source

Declaring a function with: void sort2(double *p, double *q) { takes two double pointers. When you call a function as follows: sort2(&x, &y) , you are not looking for pointers, but referring to variables. For instance:

 void sort2(double *p, double *q) { some_code; } int main(int argc, const char *argv[]) { double x = 0.2; double y = 0.4; sort2(&x, &y); //x and y addresses in memory are being passed to sort2 return 0; } 

To compare memory addresses x and y after the fact, you will need to refer to them in some form, for example:

 if(&x <= &y) do_something; 
+3
source
 void sort2(double* p, double* p) 

First. This is not a removal of links. Double * here, just to tell the compiler, this function gets two pointers pointing to a double type. This is an ad.

So, you pass the function pointer here. Memory addresses are stored in p and q in sort2.

Hope that solves your problem.

+2
source

Pointers are not links. To compare values ​​stored in pointers, the dereference operator is used, which is unary * . C ++ and C use declaration syntax that mimics usage. So double *p gives a hint that *p is of type double . Then the value *p is the value of the double object pointed to by p . If p does not actually indicate a valid double , the result of the dereference is undefined.

+1
source

Forget programming and pointers for a second, and think about a library. There are many shelves with lots of books. Each book is in certain coordinates. For example, the Tale of Two Cities may be located on Aisle P, Shelf 2, Slot 14; likewise, Jane Eyre can be on Aisle Q, Shelf 13, Slot 1. It is clear that you want books in the library to be well organized so that they can be easily found.

Now consider a librarian who is tasked with making sure that the books in the above coordinates are in the correct order. The librarian goes to each coordinate, looks at the names of books in these coordinates and determines which one should go first. In our case, the librarian would have noticed that since J comes before T, she needs to change books.

Your homework seems to be a C ++ version. The sort2 function gets two memory addresses (i.e., Book Locations), and it needs to make sure that the contents of the first location are correctly compared with the contents of the second location. (“Compares correctly,” as a rule, either “compares less” or “compares more than”, depending on whether you sort the ascending or descending.)

As for how sort2 is sort2 , this part is a bit confusing to explain, and my analogy doesn't work perfectly. The variables x and y are usually treated as data (for example, the contents of the book), but each of them also has an associated address (for example, where the book is located). If you type x , you request the contents of the variable; if you type &x , you request the appropriate memory address. Unlike books, computer memory is constantly changing (this is part of why computers are so useful, of course). Therefore, in this context, x and y better perceived as a named piece of memory, rather than names for actual data (for example, these are the names of the locations of the books, not the contents of the books). Therefore, when you call sort2(&x, &y) , the address operator explicitly indicates that you are working with locations x and y , not their data. After the call, the addresses x and y will not be changed, but their contents may have.

Hope this helps!

+1
source

In your void function sort2 (double * p, double * p), you must name the parameter name differently than sort2 (double * ptr1, double * ptr2).

Then call sort2 (& x, & y);

Inside sort2 (double * ptr1, double * ptr2) you can swap or sort whatever you want.

+1
source

All Articles