Array Internals
Really, what is an array?
– It’s a sequence of values of the same type stored consecutively in memory.
* Why do these qualities matter?
– If you know the size of each element
– … and where the array starts in memory
– … you can find the memory storing any element.
* So, to use an array, the compiler needs to keep up with:
– How big each element is, so it can do indexing
* That’s implied by the element type
– The location of the first element in memory
* That sounds like a pointer
* And, the compiler will give you this pointer if
you want it.
Arrays as Pointers
Pointer Arithmetic
Array Elements as Pointers
Pointer dereference to
access the first element.
*p = 25;
Pointer arithmetic and
dereference to access other
elements.
*( p + 1 ) = 35;
*( p + 2 ) = 75;
Array name evaluates to a
pointer, so you can even do
this. In fact, you’re in for a
surprise …
*a = 26;
*( a + 1 ) = 36;
*( a + 2 ) = 76;
Array Indexing Explained
Indexing with Pointer
Passing Arrays and Pointers
Array Parameters(Us v Compiler)
Functions we’ve covered
Array Offset
Marking your Spot
Arrays are not pointers
Useful Pointer Arithmetic
You can add and subtract pointers and
numbers
pointer + number -> pointer
number + pointer -> pointer
pointer – number -> pointer
These each evaluate to a pointer to a nearby
value.
pointer – pointer -> number
This evaluates to the number of values
between two nearby pointers.
number – pointer -> nonsense
Not all operations are meaningful.
* You can compare pointers
pointer == pointer pointer != pointer
pointer < pointer pointer > pointer
pointer <= pointer pointer >= pointer
Alternatives In Array Indexing
Alternatives In Array Indexing(part 2)
Making Arrays(2 ways)
Literal Strings
String Creation Alternatives
Command Line Arguments