[MUSIC] Okay, let's continue our
discussion of how manipulate data and
memory in the C language.
our next topic is going to be how we talk
about arrays.
basically arrays are adjacent locations
in memory that store the same type of
data object.
So here I'm in this example declaring an
array of integers.
it's name is big_array and there is 128
integers in that array.
that number between those square
brackets.
this is going to allocate 512 bytes of
adjacent memory starting at some
location.
What location?
Doesn't really matter.
The one I'm showing here is pretty
arbitrary but it will be easy for us to
use in our examples.
but basically the compiler will take care
of that and will decide where in memory
that data will go.
now our pointer arithmetic can be used
for indexing in this array.
So let's take a look at some examples
here.
First of all, let me start with just
declaring a pointer to an integer type.
and we're going to call that pointer
array_ptr.
Okay?
Now, the very next statement says, let's
take big array, and assign it to array
ptr.
Well, because that's an array, and I've
declared it as, as such.
C knows that I don't mean the value big
array, because there's 128 different
values.
What I really mean is.
The address of the first location of that
array.
So what's going to be assigned to that
pointer is the address of that first
location.
That is exactly what the second statement
is going to do.
You'll notice in this case what I've said
is go to the zeroth element of the array,
the first one.
And get me its address by using the
ampersand.
So, of course, that, that will be the
same address.
In the next case, I have now used an
index of 3.
That really corresponds to the 4th
integer in the array.
There is one at index 0, one at index 1.
One at index 2.
And the fourth one at index 3.
So how far down the, the array is that
one?
Well, this is where that pointer
arithmetic becomes important.
Since integers are of size 4 bytes.
What I really mean is at 12 bytes passed
the start of the address is where that
fourth element will begin.
So the address that I just generate here
is ff000c.
That c corresponds to 12 in decimal.
3 times 4 bytes.
Okay?
Now, that's exactly the same thing as the
next statement.
And why is that?
Because, in this case, what I've done is
first taken a, a, a pointer to the first
element.
And then said, add 3.
But remember, c knows that if we're
adding to a pointer, we don't mean just
add the value 3.
But rather, the 3 times the size of the
data element.
Or in this case, an int 3 times 4.
Okay, so now you can kind of see why C
does this in this pointer arithmetic.
What about the next statement?
Here, I'm just saying big array plus 3.
Well again, that's the s-, same situation
as the very first line, where we've taken
big array to indicate just the first
address of the array.
And the plus 3, again, is to an address.
We're adding 3 to an address.
So we're going to multiply that by the
size of the data item.
Alright, in this next example is a bit
more complex.
You'll notice that it's using 2D
reference stars, one on the left hand
side and one on the right hand side.
The one on the right hand side is saying
get the value at array pointer, and use
that as an address, and go get the value
there.
Well, what is the array pointer been set
to?
Well, we had just set it to this address
that ended in 0c.
So we're going to go into that location,
get the value stored there, add 1 to it.
Okay, and then where are we going to go
put it.
Where are we going to put the result?
Well, the result, it says, to go put in
this, at the same place where we got the
value.
So it says, go to the address pointed to
by address by array_ptr.
That star in front, again, is saying the
reference, the value in array ptr and go
to that location.
Well again the value in array _ptr is 0c,
so we going to go to that location again.
So what does this accomplish?
Well, what this has accomplished is to
increment the value stored in that fourth
element of the array.
At index 3.
but it does not change the value of the
array_ptr variable.
That stayed the same.
And it's still pointing to that address
0c.
Okay, what about our next statement.
You'll notice here we're trying to
reference the 130th element of the array.
well, our array was only 128.
What happens here?
Well turns out C doesn't care.
It just applies the same point of
arithmetic rules it did before.
In this case multiplying 130 by 4 and
adding that to the starting address of
the array, and will just give us an
address that points to that location.
That turns out to be out of the bounds of
our array.
We've gone too past the end of the array.
But C doesn't check for that.
And will see later on, other languages
do, but C does not.
And that's one of the dangers in using C.
Now, the general rule here is that if I
have an expression that looks like this.
that is the same as saying bigger A plus
I because just bigger A by itself is the
starting address.
And plus i gets multiplied by the size of
the element of the array before its added
to that address.
So that it implicitly computes this
expression.
Which you'll notice is the same thing by
saying the starting address of the array
plus i times the size of the element of
the array.
Okay, and in C we can use the size of
function to tell us what size the element
of the array is.
Okay, let's take a look now at strings
rather than intergers.
A C-style string, a character string, is
represented by an array of light.
Each byte has a code in it two digit hex
code and one of the more popular ones is
the ASCII code that uses one byte for
each character.
And here you see a map of most of the
characters in the ASCII character set.
And the way C's, indicates the end of a
string, is with a byte that only had the
value 0 in it.
So a string like Harry Potter for example
would be represented by the following
sequence of bytes.
You'll notice that there is a byte for
the space, between the two names.
the, the value, the code for a blank is
32x.
And, a 0 at the end, signifying the end
of the string.
So, the string, Harry Potter, is a 13
byte array.
There are 12 characters including the
blank, plus, a 0 at the end, to indicate
the end.
Now, the reason we put a 0 at the end,
often referred to as a null 0, is, so
that we can tell the end of the string.
Alright, how would be compute the string
length if we were given the starting
point of the string?
We would move 1 byte at a time down the
string and checking for 0.
When we find the 0, we would know we were
at the end.
And that's how we would compute the
string length.
Okay.
Because we're dealing with 1 byte at a
time, we don't have to worry about issues
of little Indian, or big Indian, when we
think about character strings.
So, if we had the character string 1, 2,
3, 4, 5, it would actually be represented
in precisely the same in little Indian
machines, or big Indian machines.
Now, later on we'll learn about unicode
characters that have more bytes per
character.
unicode is a newer standard than ASCII
that allows us to represent all the
character sets around the world.
And we need a lot more codes to do that
for all the different alphabets that
exist.
So they can reach up to 4 bytes per
character and Java has, Java as well as C
have libraries for unicode.
But Java commonly uses 2 btyes per
character that's sort of it's default.
So we'll see later the complexity of this
but for now strings are 1 bit per
character.
Okay let's take a look at the C function
that we can use to print out data that we
store in memory.
By printing it out one character at a
time or 1 byte at a time as 2x digits.
Alright, so here's a simple function
written in C and it's called show bytes.
It doesn't return a value so we've put a
void in front of it to indicate that.
And then it takes two arguments.
The first one is the address of the
starting location and memory.
So, you'll notice it has a star there
because it says this is a pointer to to a
location a memory is not a value.
And to what it kind of thing is it
pointing, well it's pointing to
character.
And then see a char is a 1 byte quantity.
So in this case, we're just saying we're
point to a byte in memory, right.
And then the second argument is an
integer that is the length of, of bytes
that we want to print, the number of
bytes we want.
Okay, the body of the function is just
the simple four loop that goes from 1 to
length or 0 to length minus 1 and
increments the index by 1 each time
around.
That is what that i plus plus indicates
increment by 1.
The body of the loop is a, is a simple
print statement that, we'll see the
format of in a second, but it prints two
values.
The first is an address, the second a
data value.
You'll notice that we're using start,
that pointer.
So this is going to be an address.
Then we're adding i, the index to it.
Since we're doing pointer arithmetic,
what do we multiple i by?
Well, since it's only pointing to a
character.
We multiply by 1 because characters are 1
byte.
And then we print the data stored at that
address by de-referencing that address.
That's why we have the star on this side.
So here we have the address.
And here we have the data at that
address.
Our print format screen says that we
start off by printing a pointer.
Okay, that would be that first element
here, that address.
So we're going to print a pointer.
Then we're going to insert a tab.
Then we're going to print the, the number
0 and the letter x.
And we're going to do that to conform to
C notation for hexadecimal values.
And then the percent 0.2x is a special
notation for saying, print the
hexadecimal value of 2 digits, followed
by a new line.
That hexadecimal value of 2 digits is
just the value stored at that address.
Okay, the second element here in our
print state.
Okay, so that's a pretty nice little
function.
And we can call it from with anything
actually, even integers not just not just
characters.
Because, when we call it here showing an
example of another function that's
going to show integers.
We can call show bytes by casting the
address of the integer as a character,
address.
Okay?
This is C casting.
What we've done is said, we have an
address here, but it's the address of an
integer.
Can you please treat that as the address
of a character and then pass it to the
function?
Okay?
And then the length here is just the size
of the integer.
So here we would expect to see 4 bytes
that are going to be printed out by the
function.
Let's take a look at some sample
executions of this function.
Here we see the function in integer
1,2,3,4,5 being placed in a, and when we
call will we'll first print out that
statement and then call the function,
show int.
So here's that first print statement,
directly from above that prints out the
value.
And then you'll see that inside of show
int, we've of course called show bytes.
And it went for 4 bytes printing out each
one in turn, successful bytes at cc, cd,
ce and cf.
And the rest of that address, you know,
is pretty arbritrary.
Again, it's just where that that integer
a happened to be.
And the, the integers.
the, the values that got printed are 39,
30, 0, and 0x.
Which we know, from previous examples,
are the rep-, the hex representation of
that value.
Okay?
So this is indicating that our machine is
little Indian.
Because we saw the least significant byte
first.
And that's how we can write a simple
program to check the[UNKNOWN] of our
machine.
Wyszukiwarka
Podobne podstrony:
05 ArraysWykład 05 Opadanie i fluidyzacjaPrezentacja MG 05 20122011 05 P05 2ei 05 08 s029ei 05 s052Arrays05 RU 486 pigulka aborcyjnawięcej podobnych podstron