424,963 Members | 924 Online
Need help? Post your question and get tips & solutions from a community of 424,963 IT Pros & Developers. It's quick & easy.

# C++ Primer ex 7.6

 P: n/a can we make it better? /* C++ Primer - 4/e * * exercise 7.6 * STATEMENT: * write a funtion that swap vales pointed by two pointers to int. */ #include int swap_values(int* ip, int* jp) { int temp = *ip; *ip = *jp; *jp = temp; return 0; } int main() { std::cout << "enter 2 integers: " << '\n'; std::cout << " i = "; int i; std::cin >i; std::cout << " j = "; int j; std::cin >j; int* ip = &i; int* jp = &j; swap_values( ip, jp ); std::cout << "values swapped: " << '\n' << " i = " << i << " j = " << j << std::endl; return 0; } /* OUTPUT [arnuld@arch cpp] \$ ./a.out enter 2 integers: i = 3 j = 2 values swapped: i = 2 j = 3 [arnuld@arch cpp] \$ ./a.out enter 2 integers: i = -9 j = 0 values swapped: i = 0 j = -9 [arnuld@arch cpp] \$ */ -- http://arnuld.blogspot.com Aug 10 '07 #1
12 Replies

 P: n/a Hi! arnuld schrieb: can we make it better? Alf said something about "const" already. But you can only use it here: int swap_values(int* const ip, int* const jp) { const int temp = *ip; *ip = *jp; *jp = temp; } Frank Aug 10 '07 #2

 P: n/a Hi! arnuld schrieb: #include [snip] std::cout << "enter 2 integers: " << '\n'; Theoretically you need "#include " for this to work. only defines cout and cin, but not what "<<" means to them. defines "<<". For a long time I thought "iostream" would include both "istream" and "ostream", but I had to learn they are completely indepentant. What I assume is that "ostream" includes "istream". I think I got that somewhere. So if you ever come along a compiler which refuses to compile your code, then include

 P: n/a On 2007-08-10 14:19, Frank Birbacher wrote: Hi! arnuld schrieb: >#include [snip] > std::cout << "enter 2 integers: " << '\n'; Theoretically you need "#include " for this to work. only defines cout and cin, but not what "<<" means to them. defines "<<". For a long time I thought "iostream" would include both "istream" and "ostream", but I had to learn they are completely indepentant. What I assume is that "ostream" includes "istream". I think I got that somewhere. So if you ever come along a compiler which refuses to compile your code, then include ) since there's no implementation of consequence out there that does not allow the OP's code. -- Erik Wikström Aug 10 '07 #4

 P: n/a On Aug 10, 4:47 pm, arnuld

 P: n/a On 2007-08-10 10:50:57 -0400, HumbleWorker can we make it better?/* C++ Primer - 4/e** exercise 7.6* STATEMENT:* write a funtion that swap vales pointed by two pointers to int. COMMENTS OF HW: This is more efficient: void swap_values (int * const& iFirst, int * const& iSecond) For pointers it's very unlikely that passing by reference will be more efficient than passing by value. It wil typically be slower, because under the covers the code has to first dereference the pointer that implements the reference, then dereference the resulting pointer to get at the actual data; passing by value doesn't require that first step. On the other hand, passing pointers by reference clutters the function definition, reducing the efficiency of the programmer who has to write the function and of every programmer who has to maintain it. -- Pete Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The Standard C++ Library Extensions: a Tutorial and Reference (www.petebecker.com/tr1book) Aug 10 '07 #6

 P: n/a Hi! Alf P. Steinbach schrieb: Would you also forbid use of std::cout? No, because it's too complex for you to implement it as an excercise. I'm not a teaching expert, but usually you go throught implementing various "small" functions like std::max yourself before using the predefined ones. So first implement my_max, then be told there is std::max. Am I wrong here? Frank Aug 10 '07 #7

 P: n/a COMMENTS OF HW: The subject of the question is a pointer, we have been GIVEN two POINTERS the contents of which have to be swapped. The pointers are GIVEN, so it is more appropriate to pass them by reference and act on them. The point is very subtle. Passing the pointers by value would be more appropriate if we were using pointers as an intermediate helpers for swapping their contents. In this case the pointers to ints are given to us as the starting point. The following would be a more appropriate demonstration of the fuction swap_values() int main(/*int argc, _TCHAR* argv[]*/) { // take any two pointers from the heap // fill them with two values int *i = new int (/*fill with say,*/50); int *j = new int (/*fill with say,*/9); std::cout << "Before swap the contents are, i = " << *i << " j = " << *j << std::endl; // swap the values in the two pointers swap_values (i, j); std::cout << "After swap the contents are, i = " << *i << " j = " << *j << std::endl; delete i; delete j; return 0; } >becauseunder the covers the code has to first dereference the pointer thatimplements the reference, then dereference the resulting pointer to getat the actual data; COMMENTS OF HW: I donot think an optimizing compliler would take that roundabout path for implementing an int*&. Copying the contents of the int*[ie the starting address of i or j] may be the approach taken. >On the other hand, passing pointers by reference clutters the functiondefinition, reducing the efficiency of the programmer who has to writethe function and of every programmer who has to maintain it COMMENTS OF HW: This has always been a matter of opinion and taste, but I think it is better to take the approach that maps to the physical problem our code is solving. In this particular case passing the pointers themselves by reference is closer to what our code is trying to solve: write a funtion that swaps values pointed by two pointers to int. Aug 11 '07 #8

 P: n/a Hi, On Aug 11, 8:56 am, Ian Collins int main(/*int argc, _TCHAR* argv[]*/) { _TCHAR*? COMMENTS OF HW: I use MSVC compiler and I already commented off the irrelevant portions of main. TCHAR is a Microsoft child, let us not bother about it here, but I have seen them commented off in many posts, so I did them. We know these are your comments. COMMENTS OF HW: I use this decoration for clarity. Why? The pointers are not being modified. I will give you a synonym: Pointer = Class Object; Pointer contents[dereferenced ones] = Class Object public member. We pass a reference to Class Object if we intend to modify its datamember back home, on the same analogy I am talking about the pointer as a reference. COMMENTS OF HW: I know that this was just a textbook exercise and we all have been through them, but it is always interesting to re-read the questions between the lines and try look at them philosophically. But I do have a point. Thanks, HW Aug 11 '07 #9

 P: n/a HumbleWorker wrote: On Aug 11, 8:56 am, Ian Collins No, it's silly. If you were given to integers to pass to a function,would you pass reference to them? We would pass them by reference if we are to act on them to modify them back home. In this case we are modifying the contents of the pointer and we are passing that particular pointer by reference. That's where you misunderstand, in this example, the data pointed to by the pointers is modified, not the pointers them selves. A pointer stores an address, it is the contents of the address that changes, not the address. If the function were to change the address pointed to by the pointer, it would have to be passed by reference. > >We know these are your comments. COMMENTS OF HW: I use this decoration for clarity. You don't have to, news readers no how to quote. Your decoration only adds noise. -- Ian Collins. Aug 11 '07 #10

 P: n/a On Aug 11, 12:31 pm, Ian Collins I will give you a synonym: >Pointer = Class Object;Pointer contents[DEREFERENCED ONES] = Class Object public member. COMMENTS OF HW: Anyhow let us move on. HW Aug 11 '07 #11

 P: n/a Do you want a more interesting algorithm to implement the task to swap two values? Here it is, but I think it is just a trick void swap(int *ip, int *jp) { *ip = *ip ^ *jp; *jp = *ip ^ *jp; *ip = *ip ^ *jp; } int main() { int i = 3, j = 4; swap(&i, &j); cout<

 P: n/a On Aug 11, 2:42 pm, wei

### This discussion thread is closed

Replies have been disabled for this discussion.