Without writing the program, can you predict what it will print at the end? Then, write the program in
functionExample.c, and confirm.
Run the code and observe its output. Draw a memory diagram on your worksheet indicating the stack's contents just before
f2()returns. (Use the stack model described above, remember to ignore printf statements). Confirm that our stack model accurately represents the program's behavior.
Change the program so that
f2()is called beforef1(). How does the program output change?
Revert so that
f1()is called first. Then change the program so thatint mis initialized to 3 in its declaration inf2(). Does our stack model still accurately represent the program behavior? If not, can you guess what the compiler is doing differently?
Revert so
int mis not initialized, andint n = 4. Does our stack model still accurately represent the program behavior? If not, can you guess what the compiler is doing differently?
What is the compiler error generated?
Add function prototypes to the top of your
functionExample.cfile and verify this removes compiler messages whenint mainis moved to the top.
Implement the above in
swapExample.c. Draw a complete memory picture just beforeswap()returns.
What happens in each of the following two modifications of the
swapprogram? Draw a complete memory diagram on your worksheet. Explain why neither of them work.
Implement the above in
structExample.c. Define another pointer based location struct calledmyHousePtrand make it point to themyHousevariable. Add another print statement that outputs the values for your new struct.
What errors do you get if you try to use the wrong operator to access either a pointer or non-pointer based stack?
Add a while loop at the end to print each node's data in turn using a pointer that successively points to each node. Write your code in
structExample2.c
Fill in the worksheet memory diagram for the stack and the heap immediately before your while loop executes. You do not need to fill in addresses, but you should draw arrows showing what any pointer variables reference, or write
NULLfor the contents of any uninitialized pointers.
Hand-execute the code and predict what gets printed out in your README.
Do you think
staticvariables are stored on the stack? Why or why not?
In
mathproblem.cwrite a method that computes the sum1 + 1/2 + 1/4 + ... + 1/2n
for any input parameter n.