Tác giả giữ toàn bộ bản quyền (All Rights Reserved) — không phải giấy phép mở. Bạn vẫn xem và học miễn phí trên OpenER; muốn tái sử dụng cần xin phép tác giả.
PROFESSOR
And welcome back. Let's now talk about C arrays. So arrays are similar to what you've probably seen in Java. You declare them by saying with type it is and how many you want.
So here, the first line says int ar of 2 says I want two integers side to side.
The most important thing to understand about arrays is that it's a way of asking C for a contiguous block of memory. I need to have two integers side by side.
You can't have them here with a gap in the middle. You have to them together. You can also declare and initialize them at the same time without saying the space.
So ar open close square bracket equals two numbers, initializes those two spots, and says it's going to be two of them.
And the way you access them is the square bracket notation you've seen from Python and from Java, ar of num where num is some value. And it's 0 index.
That's really important. So mostly arrays and pointers are identical actually. They're mostly the same.
So if I say char *string or char string open square bracket, the first says that string is a pointer to a character array, I even say character array.
And the second says that string is an array. So first the string is a pointer to a contiguous block of characters, and the second is that string is a character array.
They're nearly identical. People often almost always even confuse them in the way they describe them. What's different is that you cannot increment an array. The open square bracket, you can't increment it.
You can't move the pointer. You can for the pointer, but you can't. So if you have a big array and you're moving across, you can't increment it if you have open square brackets.
It's very subtle. The way you declare a filled array, you [INAUDIBLE] with the guy on the right, not the guy on the left.
But you can think of it as an array of variable points for the first element.
I mean, they're also subtle in some other ways of where they're stored, and I'll talk about that in a second, but that's the big idea. So mostly they're the same.
Mostly they're the same. What that means is if you have ar and it's an array variable, but it looks like a pointer in most respects.
So you can say ar of 0, you can also say *ar. So remember ar was declared as an array. You can still say *ar. You can also say ar of 0.
You can say ar of 2, or you can also say ar plus 2.
This is the first time I've shown you pointer arithmetic, and the plus 2 says, look, whatever ar is pointing to, I don't want that one, the zero element, I don't want the next one.
That's the first element. It's not the first, it's the one element index. I want the 2 index. I want 2 over.
So you say, well, Dan, how does it know how wide they are? How much to increment the pointer to increment that? How far to go across to get that value.
That's what types are about. That's why you told it what that pointer was to, that array was for.
The declared space of an array is only valid, they're temporary, while the scope of that procedure is valid. So here's char *foo. I'm making a new character, I'm make a new subroutine called foo.
Pretty cool. I'm going to return a pointer, and then I say character of string of 32. I'm making now some local storage.
Array of 32 characters calling it string, and I'm going to return that. You're not supposed to do that because that space is only available in that subroutine.
So only while foo was alive was that string available, and the moment foo ends, the system might reuse that so you don't have access to that. That's a key error.
We're going to mention that many more times, but don't do that. Don't do what we did here in foo. You don't have access to that once the procedure subroutine returns.
I mentioned before arrays are indexed, so if the array size is n, you want to access 0 to n minus 1. That's your range of things you can get to.
The problem is this particular slide is talking about a style thing. This is really a style thing here. So the top line of code looks right. Doesn't this look right?
Look at it, int i ar of 10. For i is 0 less than 10 i++, looks good, right? Well, the problem is you've written 10, you've kind of hard coded 10 twice.
And what we want to teach you with 61C is not just how C works, but actually good style.
So one of the things we want to make sure, inculcate, in this class is that you understand that the single source of truth is a valuable thing.
That's the reason why Google Drive and Google documents are so great. You have a single source of truth and 25 people are doing it rather than passing out version final, final, final.
No, version, and oh. version 17. Crazy. We look back and we ask ourselves, why did we ever do that?
Same thing here in C. Once you realize that you're going to have 10 hard coded, the hair on the back of your neck should go up.
And you should say, you know what, I shouldn't do this. How do I do this better? Well, one way to do is is to say int array size is 10.
Before an ANSI days, before C99, you had to have pound sign array size 10. Pound sign define array size 10.
You had to global define this as a pound defined in a C preprocessor directive. Here int array size equals 10. So int i, a of array size. This is a dynamic array.
Again, this came from C99. And now I can say for i equals 0, i listen to array size. Now I only have one 10. I want to now change it.
I want to make it 20. Now I change it one place.
So look at this and look well, young padawan, as you understand the ways that — the pro tip for how to do this. Any hard coded value, you have it there once.
Abstraction. So use indirection, indirection can save you a lot of trouble and you don't have to maintain two copies of 10. Here's the other thing. Arrays don't know when their bounds are being exceeded.
This is C. If there's ever a question of whether C checks for something, the answer's always no. C doesn't do any of those checks.
C says I'm just going to let you run as fast as you want because basically I'm going to compile it down to raw machine code. I'm not going to do those checks for you.
Those sanity checks. Make sure you're between 0 and n minus 1. No, doesn't know that.
The other downside is an array doesn't carry with it its size, so that's a little bit of a bummer.
So if you ever pass an array around, you have to pass the array and you've got to pass the size of the array, or at least the number that you're going to be searching up to.
You can pass an array of size 100 if you only pass 10, and then only think it's actually only 10 long, and it won't go past that.
But you need to kind of pass these two numbers along. There's only one small asterisk to it, exception, which is strings.
Because we've all agreed that 0 character is a special way to terminate a string. So if I'm passing a string to you, you can keep reading until you get to 0.
You don't have to pass the string and the number. Now you can also write the number and then you stop short of that.
But strings you don't have to pass because we've all agreed that 0 is a null terminator at the end of a string.
But every other array, every other array, you have to pass in the array and the size, or at least the size up to you want to be checking.
You're going to see some errors as your coding in C. You wouldn't be learning if you weren't finding errors.
You're not learning how to ride a bicycle if you don't scrape your knee once or twice. We've got to kind of try. Wiggle the wheels. What if I turn really steep?
Well, you hurt yourself and you fall. But if I pedal while I'm making — I did this pedaling while I'm making a turn, well, all of a sudden the pedal hits the floor.
So you got to try all these things to know what the bounds and range is so that when you get to a dangerous situation, you won't do that.
So segmentation faults and bus errors are two kinds of errors that you'll see. The idea of — I'll just give you a little quickie.
A segmentation fault says you're reading and writing to memory you don't have access to. You're reading outside the segment. The segment kind of is a way to your access space.
A bus error often means your alignment is wrong. So we talking about word alignment a couple of videos ago? [INAUDIBLE] read an integer.
If your integer pointer doesn't have — if you say what's the address of that integer?
If that integer isn't, the address, if you look at it in x, isn't 0, 4, 8 or C, you're not word aligned. That's in a 32 bit machine.
So then if you read an integer — I'm reading an integer, but the actual pointer is 5, the actual address of that integer is 5. Well, that's not 4, 8, 12.
It's not a multiple of 4, so that would be a bus error. If it's in the space with an error.
It can be ambiguous, where if I read it off alignment, but I'm also off the area I'm supposed to be reading, you can have one of the two.
So those are two ways. If you see those arrows, that's what they mean. Segmentation fault means I'm outside of memory I have control of. The bus error is probably not aligned.
I mentioned before pointer arithmetic, which is if you have a pointer to something and you say plus a number.
I mentioned *ar plus 2 goes to the second element in that array however big they are. It just moves over, just strides however big they are. And that's what size of was four.
So pointer plus n says, well, if it's a pointer to something, it adds. Here's a thing size. Size of character is one, so it's n times the size of character.
So it says I'm going to move that many bites across to the next guy.
So if it's an integer 4, if I say pointer plus 1, and pointer is 1,000, say pointer has to have a multiple of 4, pointer of 1,000, pointer plus 1.
This means now the pointer is going to be, if I ask addresses, it's 1,004. Pointer plus 1, again, it'll be 1,008. So it's moving by the size of that element.
That's why those types are useful to know how big to move when you say pointer plus or minus. Pointer minus means go backwards, go back in the array.
So pointer plus and pointer minus does that. Kind of a useful thing. So you saw this. It's review.
We saw this a couple lectures — last lecture. If I have a subroutine add 1 and I have y declared outside that scope and I pass in y, y is 3 initially.
Add 1 is not going to affect y. It's still going to be 3 after the call to add 1.
The way you fix it is you pass in the address of y, and now that pointer can manipulate y. And when it's all done, y is now 4.
That's all review. What happened what if I want to change a pointer? So let's look at this code here. Let me pull this out and see if I got this here.
So I've got increment pointer. Increment pointer here. I'll color my drawing here. Increment pointer — son of a gun.
There. Okay.
I've increment pointer.
What's it do? Well, let's look at it. Let's start here. It's supposed to move the pointer across.
So here's a of 3. Make a nice filled array.
By the way, you can also — I didn't mention this before — you can fill the array and also tell how big it is, as long as they're consistent.
That's critical. So 50, 60, 70, that's a nice array. I have a pointer queue. We can think of q and a both point to the beginning of that array.
So here's my picture over here indicating that. Then I called increment q. Increment pointer of q. I really want q to move over, so I want q to be here and point to there.
What I want is for there to be my q, okay? So let's now call this. Well, what would it do? int *p, I pass in the pointer to p.
Now p and q are the same thing, and I say p equals p plus 1, and I'd hope that q increments. Well, what do you think happens? Let me try this.
Erase this. What happens? 50. Doesn't move at all.
Why? Same reason as the slide before. You can't change q without passing it's — let's all on three. 1, 2, 3.
Address. So you pass in q's address. Well now, what is the type for the increment pointer that's receiving that? It's not a pointer to an int.
It's a pointer to a pointer to an int actually. H, and by the way, we call h as we call this a handle. So it's a pointer to a pointer.
H is a handle. So now when it gets [INAUDIBLE]. Let's try it. So now q, I pass in the address of q, now I can change q, **h.
The way you get to that, the way you dereference that handle, is you say *h. This is going to move where q is, one over. So let's try this.
I run this, q moves 1 over, and I print f *q. So this is the same. This didn't change down here. The print f didn't change down there.
It's just that key thing was different and that key different was different. I also changed this as well. So now same idea.
And you can say, well, Dan, look, what if I have a pointer to a pointer to a pointer? The answer is yes.
I'm going to short circuit you and say you can have a pointed to a pointer to a pointer, three stars. Pointer to a pointer to a pointer, four stars.
You typically don't have more than two stars in the code. And if you have three, either you know what you're doing and it's really complicated code, or you probably don't need to have it.
So think about trying to remove that if you can, but it gets more complicated once you have three layers. But people have handles all the time, which is a reasonable thing to do.
We'll see you in the next video.
Đăng nhập để đánh giá học liệu này.
Đăng nhập để bình luận — chưa ai bình luận về học liệu này.
Không có khóa học liên quan.