Indeed, swap in your printout there is just defined as now taking *a and *b and then it also uses the * later, but we'll come back to what the different uses of the * means, but for now I claim conceptually it just means swap has access to the locations of its parameters.
的确,在打印资料中的swap被定义为,携带*a和*b,之后它也是带*号的,但是我们将说明*的不同用法,现在,我断言swap可以,使用参数的地址。
I've decided that I need to be able, in my program, to swap the value of two variables. All right.
我觉定在我的程序中,我可以,交换两个变量值,好的。
In the second case, I found in the next smallest element and moved here, taking what was there and moving it on, in this case I would swap the 4 and the 8, and in next case I wouldn't have to do anything.
在第二次遍历中,我找到了,第二小的元素,把它移到这里,把这里原来的元素移到哪里,在这一次遍历中,我会把8和4交换,然后一次遍历,不会做任何事情。
But the moment swap returns, the moment we hit this bottom most curly brace, what conceptually happens in memory?
但是swap返回时,当我们抵达花括号底端是,在内存中发生了什么?
So, in this program when we call this version of swap 1 and 2 do not end up here, right?
所以,在这个程序中,当我们调用这个swap版本1,和版本2,不是在这里结束,对不?
Now finally swap has the ability, the power, to modify memory that isn't his own, that's not in his own scope because we've passed it in by address.
最终swap有能力,来修改不属于它的内存,不在它的作用域中,因为我们是通过地址传递的。
And if it sees S-W-A-P, open parentheses, close parentheses, and it hopefully sees two ints inside of it, that's all it needs to do in order to finish compiling your code.
如果它看到了SWAP,左圆括号,右圆括号,它可能载里面看到两个int数,那是它所需要做的,用来完成代码的编译。
Now if you call a function, swap like increment or cube or swap, or in this case, foo, those variables are the parameters to that function, end up getting stored next in memory.
现在如果你调用一个函数,像increment或者cube或者,或者在这个例子里,foo,这些变量都是,函数的参数,在内存中存储。
It's in a file called swap.C. Everything should be alphabetical so it should be toward the end of this week's packet and notice that it's almost identical except for a couple of syntax changes.
在一个叫做swap。c的文件中,对于这周末的数据包来说,所有的东西都是字母,请注意,除了几个语法改变,它几乎是一样的。
Well, simply with the * notation at least on the way in when you declare the function called swap, you simply say this is not going to take an int and another int because that's useless.
好的,简单说,当你声明一个函数调用swap函数时,使用*符号,你可以简单说,这不能使用一个int数和另外一个int数,因为那是无效的。
5 6 1 2 3 So it's 4, 5, 6 comma 1, 2, 3 that are passed into swap and that's why those same numbers end up in A and B. Yeah?
所以这里是,这就是为什么这些数字最终在a和b中,请说?
int Y Well, I said int X and int Y; so that gave me one square here called X, one square here ; or wherever, called Y, done, one was put in here; two was put in here, and then I called this function swap.
好的,我声明了int,X,和;,然后我这里有个正方形叫做X,一个正方形,叫做Y,完成,1放在这里;,2放在这里,然后我调用这个swap函数。
When I hit that curly brace, swap's role in life is over.
当我以那个花括号结束,swap的角色就结束了。
I get to the end of the loop, and I actually do a little bit of wasted motion there. And the little bit of wasted motion is, I take the value at i, store it away temporarily, take the value where min index is pointing to, put it back in there, and then swap it around.
我走到了循环的末尾,事实上我做了多余的动作了,有点多余的是我把i的值,临时保存起来了,看看MinIndex指向的值,放回到哪里,然后再交换。
So, what must be going on in swap?
那么,在swap中发生了什么呢?
So pictorially, it's as though there's now some other part of the blackboard here, maybe way up here where I can put anything I want that's accessible to main, that's accessible to swap, that's accessible to increment or any other function I might implement because it is, in fact, global.
绘画般地,它是在这里黑板上的其他部分,可能在这里的上面,我在哪里可以放置这些东西,使它们可以被main函数使用,可以被swap函数使用,可以被increment函数使用,或者我所实现的其它函数,因为它实际上是一个全局变量。
X That's just the special symbol in C that says don't pass X, that is don't pass a copy of X. Rather figure out where x is in memory, where he is in that frame and provide swap the numeric address in RAM of that value so that swap can go do anything it wants at that address.
这在C中是一个特殊的符号,指明不传送,也不是传递X的拷贝,而是指出x在内存中哪个地方,它在那个框架中的哪个地方,提供给swap它的地址值,这样swap就可以使用那个地址中的数据。
So this is version two of the swap function and, in fact, it is almost identical, this is before, this is after, before, after and it looks like the solution to this problem that we've been revisiting a couple of times now is just to do what?
这个swap函数的第二个版本,实际上,这几乎是一样的,这是之前的,这个之后的,之前的,之后的,这看起来像是这个问题的解决方案,我们一直多次访问它,目的是什么呢?
And now the point just to tease apart, I can't erase that, let's fix, so now the one detail that's worth pointing out is * that this here the * in swap's prototype and the * here in swap's prototype do not mean go there.
现在我们要梳理一下,我不能清除那个,让我们修正一些,需要指出的一个细节是,在swap原型中的,意思不是定位到那个地方。
I'm reminded of the confirmation screen here and that's not too bad, then we put in the card and we click confirm and then finally we see the swap Then finally literally nine steps later the ticket comes out of the machine and then, frankly, had to go through the process of where to put the ticket in the machines.
这里出现了确认画面,那还不是太坏,然后我们把卡插进去,点击确认,然后我们看到了那个,函数。,swap,function。,【笑声】然后最终经过9个步骤,票从机器中出来了,然后,需要找出从哪里,把票放到机器中去。
应用推荐