- Brainstorm and begin project 2.
- Complete pointers assignment.
- Finish Motor Lab.
OVERVIEW:
Today we learned about the concept of higher dimensional arrays and how they can be utilized to store different types of information---such as x-, y-, and z-coordinates. We also learned about the relationship between variables, pointers, and addresses.
CLASSWORK:
Variables, Pointers, & Addresses:
This program demonstrates the relationship between pointers, variables, and addresses. It displays the contents and addresses of ints a and b as well as the pointer ptr;
This is the ouput for the aforementioned program in Repl.it. I ran it three more times to see if there was a difference.
Things I noticed:
- The pointer ptr always points to the value addressed to a, which is 1.
- The memory address for integers appears to be nine digits long.
- Only the last 2 digits of each address appear to be changing.
- The value of ptr is the memory address of a.
- The address of ptr is uniquely different from the addresses of a and b.
I also ran this program in Virtual C. Here are the results.

I ran the program four times, but the address numbers stayed the same.

Conclusion:
Different compilers have different methods for assigning address numbers to data types.
HOMEWORK:
Pointers HW:
Given this information:
int i1, i2;
int *p1, *p2;
...
i1 = 5
p1 = &i1;
i2 = *p1/2 + 10;
p2 = p1;
What is the value of il, i2, *p1, and *p2?
First, I edited the program we wrote during class. Then I analyzed the code.

Prediction:
In line 10, i1 is assigned the value 5. In the next line, p1
is assigned the value of the integer memory address associated with il. This
implies that *p1 points to the value associated with i1, which is 5. In line
12, the integer i2 will be assigned a value ten more than half the value
pointed to by *p1. Since *p1 points to the value of 5, i2 would be assigned the
value (5/2) + 10 = 12.5. However, since i2 is an integer, the program will
either round it up to 13 or round it down to 12 depending on the compiler. Line
13 asserts that p2 will be assigned to the value of p1, which is the integer
memory address of i1. This statement implies that *p2 will also point to the
value pointed to by p1. Therefore, *p2
point the value i1=5.

I ran this program three more times in Repl.it to see how it would change.
Similar to the program we analyzed during class, the last two integers in the addresses change, while the numbers assigned to each variable i1 and i2 remain the same. I also ran this program in Virtual C. Unsurprisingly, the outcome was extremely similar to the program we ran during class as well.
Much like the program we ran during class, both the address numbers and the values assigned to each integer i1 and i2 remained the same.










No comments:
Post a Comment