Taught By

Ira Pohl

Professor

Transcript

So, our next topic is to show an example in C. We're going to show an important example. It's going to be trivial but still it will it's one of these helper functions that everybody learns to write. And you are going to want to swap two things, the function want's to swap two things, and typically there might be two answers two doubles or two choice. And everybody learns a C program. So again this is revealed to you. How to do that in C? The critical thing is to remember c is a call by value language. Call by value means that arguments pass in to a function. While they may be altered internally in the function when they come out and the function passes control back to wherever it was called, are not, are not changed. So as arguments are handled locally, but the actual parameter itself would have been unchanged. So when we're doing a swap of an i and a j, and we want the values to change, In a call by value language, we have to use what's called indirection, since we don't have what, and we'll turn out to have this in C++. We'll have a more, an easier way to do in C++, what we need. Add value here as well. So the classic way you will have learned this and we'll see it in the code, is to use pointers. And this is always a big stumbling block for beginners because it's a sort of metal level and you have to get used to knowing what a address is And distinguishing the address of the variable from the variable, and from the variable's contents. And that does slip people up. But this is something you should all be familiar with. And if you had trouble with it in the past, I think you'll appreciate that C plus plus makes it easier. But you also need to go back, experiment, make sure you fully understand this. So, we'll first do this as a C program, and then we'll show you how to convert it. So here's a classic swap, and we look at these declarations. And the way you read those declarations is star. Is a pointer. So this really gets read as pointer to int. It's part of it's data type. So j is a pointer to int. As is i. So, we are really passing in two addresses. Pointers are variables which can take addresses. The addresses also have to be an address of something. Like an nt. Why do they have to be an address of something? Because. Knowing an address still doesn't let us interpret the content stored at the address. We have to understand what the bits that are stored there mean, and the bits will mean different things if they're from different domains. So doubles have a different way of being read from ints and chars. So again, this is the classic C idiom for passing parameters that need modification. What happens is internally you create a temporary variable. And then you, in this case, the stars here. Star means dereference. So that's saying Look at the address represented by j. Go and steal its contents, whatever is stored there. Think of it as a letterbox, whatever is sitting in the letterbox, and shove that contents in the address that i represents. So contents is being moved between two addresses. And then finally, in the standard swap, you have to keep track of, you need a third variable. That's what temp's for. So there's an address of an item, which we might call the address that I points at. And address of an item that j points at. We've created a temp. And if initially 5 was here and 6 was there. The temp is going to get 5. And then 6 is going to end up here and then the temp values is going to be shoved into and so we're going to end up with. I pointing at something containing six, and j pointing at something containing 5. That's something again that should be reviewed. So, this is an assumption that you've already been programming in this language. For a while and you have facility with this concept. Now what if I want to change the domain of swapping? In the sea language, if I now want a set of swapping images, swapping double, I have almost the same prescription. But there are a couple of things that change. One of things that I need to make sure of is I have to give the swap of the double a new name. So I can't have it named swab. Of course have different arguments. Appropriate to the domain. And then the code looks almost the same, except again, we have a type that's different. So what gets changed is the function name, because in C Within scope, all function names have to be unique. Otherwise it would be ambiguous in the C language. And we have to write the code specific to the type. Now, we're going to see later on, in C++, that we have very powerful mechanisms for code reuse. That allows us to even just write one swap function. Keep that one swap function with the name swap, and have it apply to arbitrary types. That's going to be very powerful, and won't involve any more programming difficulty, and in some cases may be easier. Then what you would have to do in C. So here's the use of those swap functions in a C program and again this is review. So this should be an idiom you fully understand. Here's how we print out stuff. So here we print out m and n. We need to know formats for it. So, this is going to print 5 and 10. Then we're going to perform the swap. And in the swap we use ampersand, which everybody should know is the address operator. So this is going to say M is a variable, but I need, because I want, in effect values to be changed. Under the rigorous call by values Samanthix of c after passing the actrices there. The actrices won't be changed, but the contents of what the actrices point at can changed. And that was the whole business of using pointers. The underline pointers, so. This lets, this gets us the type we want. It's a pointer to int. It's an address of an object int being stored at the location representing m. Similarly for n. And then a swap occurs. And then if everything worked right The resulting output would be 5 and 10, and then the same thing would happen with double. And so, double would have 5.3, 10.6 and again with double, you're seeing that we're using long, floating point. These formats. In order to do our IO. Again, it's something you have to learn when you're doing the C language, because the C language is not necessarily type safe. Now you can see what happened, just repeating what I just told you about. M started with 5 and 10. Swap was called with addresses of M and N, using the address operator. And then at the end, using temp as the intermediary in the function call, you get 10 and 5 occuring. You get to switch or the swap with 10 and 5. So summarizing, C, when you want what we might call in general programming methodology, call-by-reference, rather than call-by-value, has to be, uses, simulation with pointers. We have call by value of course, but because we're using pointers in direction, we get the effect of what would be called in other languages, call by reference.

Explore our Catalog

Join for free and get personalized recommendations, updates and offers.

Coursera provides universal access to the world’s best education, partnering with top universities and organizations to offer courses online.