Arrays as Pointers 10 26 2006 1 Opening Discussion What did we talk about last class Do you have any questions about the reading Do you have any questions about the assignment Minute essay responses 2 Arrays as Pointers I ve said many times that 1 D arrays are basically just pointers Let s explore that idea a bit to see how things are laid out in memory What do we get if we print the name of an array What if we print the address of it What are the addresses of the various elements in an array What are the differences in behavior between a pointer variable and an array variable 3 Using Pointer Arithmetic I ve shown you several times how we can do arithmetic on pointers This gives you low level control to look through memory on a machine Pointer arithmetic behaves properly for the type of things that the pointer points to Let s write a loop that prints the contents of an array but does it without ever using This type of code isn t really needed with modern compilers but is definitely worth understanding 4 Multidimensional Arrays What does a 2 D array look like in memory How is a 2 D array different from a pointer to a pointer Let s using some print statements to examine the structure of a 2 D array in memory and use that to compare that to the image of how a pointer to a pointer could act like a 2 D array 5 Dynamic Memory The arrays that we have dealt with have some serious limitations Most notably they have to have a fixed length to adhere to the C standard Having to specify the length of everything but the first dimension in a multidimensional array is also a bit limiting The way we get around this in C and many other languages is to use dynamic memory Dynamic memory is memory that we can request from a different part of memory Normal arrays get placed on the call stack in the normal stack frames Dynamic memory including arrays comes from the heap 6 Minute Essay Write a function that will tell you if two strings are equal The trick is that the function can t use Remember that assignment 5 is due today 7
View Full Document